assembly - Flip contents of EAX register -


i'm attempting create program (using assembly) allows enter word encrypted, followed decrypted.

the current code i've got gives me reverse order of i'm after.

http://i.imgur.com/kgdiba0.png

currently eax register 00000037.

i want 00000073.

how can achieved?

thanks in advance.

here code flip eax content, made gui turbo assembler x64 (http://sourceforge.net/projects/guitasm8086/), :

  1. assign big number eax = 1234567890.
  2. display eax (convert eax string , display message).
  3. flip eax = "0987654321" (extracting digits 1 one , storing them in string in reverse order).
  4. display string (eax flipped) message.
  5. convert string number in eax = 987654321 (this eax reversed).
  6. display new eax (convert eax string , display message).

and here :

.model small  .586  .stack 100h  .data  msj1   db 13,10,'original eax = $' msj2   db 13,10,'flipped  eax = $' msj3   db 13,10,'new      eax = $'  buf    db 11         ;max number of characters allowed (4).        db ?          ;number of characters entered user.        db 11 dup (?) ;characters entered user.   .code           start: ;initialize data segment.   mov  ax, @data   mov  ds, ax  ;convert eax string display it.   call dollars  ;necessary display.   mov  eax, 1234567890   call number2string  ;parameter:ax. return:variable buf.  ;display 'original eax'.   mov  ah, 9   mov  dx, offset msj1   int  21h    ;display buf (eax converted string).   mov  ah, 9   mov  dx, offset buf   int  21h    ;flip eax.   call dollars  ;necessary display.   mov  eax, 1234567890   call flip_eax  ;parameter:ax. return:variable buf.  ;display 'flipped eax'.   mov  ah, 9   mov  dx, offset msj2   int  21h    ;display buf (eax flipped converted string).   mov  ah, 9   mov  dx, offset buf   int  21h    ;convert string number (flipped eax eax).   mov  si, offset buf  ;string reverse.   call string2number   ;return in ebx.   mov  eax, ebx        ;this new eax flipped.  ;convert eax string display it.   call dollars  ;necessary display.   call number2string  ;parameter:eax. return:variable buf.  ;display 'new eax'.   mov  ah, 9   mov  dx, offset msj3   int  21h    ;display buf (eax converted string).   mov  ah, 9   mov  dx, offset buf   int  21h    ;wait until user press key.   mov  ah, 7   int  21h  ;finish program.   mov  ax, 4c00h   int  21h             ;------------------------------------------  flip_eax proc   mov  si, offset buf  ;digits stored in buf.   mov  bx, 10 ;digits extracted dividing 10.   mov  cx, 0  ;counter extracted digits. extracting:        ;extract 1 digit.   mov  edx, 0 ;necessary divide ebx.   div  ebx ;edx:eax / 10 = eax:quotient edx:remainder. ;insert digit in string.   add  dl, 48 ;convert digit character.   mov  [ si ], dl   inc  si ;next digit.   cmp  eax, 0     ;if number   jne  extracting ;not zero, repeat.    ret flip_eax endp    ;------------------------------------------ ;convert string number in ebx. ;si must enter pointing string.  string2number proc  ;count digits in string.   mov  cx, 0 find_dollar:                                             inc  cx  ;digit counter.   inc  si  ;next character.   mov  bl, [ si ]   cmp  bl, '$'   jne  find_dollar  ;if bl != '$' jump.   dec  si  ;because on '$', not on last digit.  ;convert string.   mov  ebx, 0   mov  ebp, 1 ;multiple of 10 multiply every digit. repeat:          ;convert character.                       mov  eax, 0 ;now eax==al.   mov  al, [ si ] ;character process.   sub  al, 48 ;convert ascii character digit.   mul  ebp ;eax*ebp = edx:eax.   add  ebx, eax ;add result bx.  ;increase multiple of 10 (1, 10, 100...).   mov  eax, ebp   mov  ebp, 10   mul  ebp ;ax*10 = edx:eax.   mov  ebp, eax ;new multiple of 10.   ;check if have finished.   dec  si ;next digit process.   loop repeat ;cx-1, if not zero, repeat.    ret  string2number endp  ;------------------------------------------ ;fills variable str '$'. ;used before convert numbers string, because ;the string displayed.  dollars proc   mov  si, offset buf   mov  cx, 11 six_dollars:         mov  bl, '$'   mov  [ si ], bl   inc  si   loop six_dollars    ret dollars endp    ;------------------------------------------ ;number convert must enter in eax. ;algorithm : extract digits 1 one, store ;them in stack, extract them in reverse ;order construct string (buf).  number2string proc   mov  ebx, 10 ;digits extracted dividing 10.   mov  cx, 0 ;counter extracted digits. cycle1:          mov  edx, 0 ;necessary divide ebx.   div  ebx ;edx:eax / 10 = eax:quotient edx:remainder.   push dx ;preserve digit extracted (dl) later.   inc  cx  ;increase counter every digit extracted.   cmp  eax, 0  ;if number   jne  cycle1  ;not zero, loop.  ;now retrieve pushed digits.   mov  si, offset buf cycle2:     pop  dx           add  dl, 48 ;convert digit character.   mov  [ si ], dl   inc  si   loop cycle2      ret number2string endp    end start 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -