Assembler code for encrypting a number


Peter uses this program to experiment with assembler code. He converts a secret number (5683063) digit-by-digit to an encrypted number. The first encrypted digit (4) is the zero-based position of the first digit (5) in the first row of digits in the two-dimensional array. The second encrypted digit (4) is the zero-based position of the second digit (6) in the second row of digits in the two-dimensional array. Similarly, each of the remaining five encrypted digits is the zero-based position of the original digit in its row.

The Program

program Encrypt;
    Copyright (c) 2011 Peter Hearnshaw
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
  {$ASMMODE INTEL}//Delete this line in Delphi
  i : integer;
  theKey, fbyte, currentNumOfSecret, newValue, tempAddr, ArrValue : byte;
  total : array [1..7, 1..10] of byte = (
  newEncodedNum : array [1..10] of char;

  secret := '5683063'; //Put in your secret number here
  for i:=1 to 10 do
    newEncodedNum[i] := chr(0);
    push 7   //i am using the stack as a store like that of a count register
                    //of the @goThroughRowSecretNum: loop
    mov dl, 7
      lea eax, secret              //this...
      add eax, dl
      mov edx, [eax]
      sub edx, 48
      mov currentNumOfSecret, edx       //To this - is the code to put the current char
                   //of the secret number (we are going through each char of the secret number) into currentNumOfSecret

      mov eax, 0
      mov ebx, 0
      mov ecx, 0
      mov edx, 0
      mov tempAddr, 0
      mov ArrValue, 0
      mov newValue, 0
      mov fbyte, 0
      mov cl, 10
      mov ebx, 0

        @goThroughRow:  //go through row of array of Total
          //Go through each element
          lea eax, total     //-1 is to make sure 0 value of array is used
          add eax, 10
          sub eax, cl

          //this bit makes sure we look to the correct row of the array
          pop edx    //pops off the value of the count into edx, so it can be used from the register
          push edx   //pushes the value of count onto the stack so it can be used again
          sub edx, 1
          imul edx, 10
          add eax, edx
          mov eax, [eax]
          mov ArrValue, eax
          //END go through each element

          //newValue equals index containing value in al
          mov al, currentNumOfSecret
          cmp ArrValue, al
          jne @finish
          add newValue, 10  //this
          sub newValue, cl  //and this line are used to give the value of the index rather than the position
                            // of the element from the end of the array

           //END newValue equals index containing value in al
        loop @goThroughRow
      pop edx   //pops value of count into edx where in line 84 it will be decr.
                 // Then pushed again to continue the iteration
      lea eax, newEncodedNum   //put address of the new encoded number array into eax
      add eax, dl    //add the offset of where we want to add the new number character into the array
                     //This is because we are working char by char through the secret number and putting the encoded value into the new array
      add newValue, 48   //start newValue of at ascii 0
      mov bl, newValue
      mov [eax-1], bl  //put the newValue of the char into the address of the offset of the newEncodedNum array

      add theKey, 1
      dec edx
      push edx
      cmp edx, 0  //iteration depends on edx which is the temporary register store
                        //of count which is usually kept on the stack.
    jg @goThroughRowSecretNum


  for i:=1 to 10 do         //write out the new encoded number array


Can you think of a program like this that you could write?

Programming - a skill for life!

Fourteen programs (with five web versions) including 3D-Driving, GASP and Knowledge by Peter Hearnshaw