翻转EAX注册内容

时间:2015-04-23 05:52:14

标签: assembly

我正在尝试创建一个程序(使用程序集),允许您输入一个单词,然后加密,然后解密。

我得到的当前代码给了我与我追求的相反的顺序。

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

目前EAX寄存器为00000037.

我希望它是00000073。

如何实现这一目标?

提前致谢。

1 个答案:

答案 0 :(得分:0)

以下是使用GUI Turbo Assembler x64(http://sourceforge.net/projects/guitasm8086/)制作的翻转EAX内容的代码,这就是它的作用:

  1. 为EAX = 1234567890分配一个大号。
  2. 显示EAX(将EAX转换为字符串并显示消息)。
  3. 翻转EAX =“0987654321”(逐个提取数字并以相反顺序存储在字符串中)。
  4. 显示此字符串(EAX翻转)并显示一条消息。
  5. 在EAX = 987654321中将字符串转换回数字(这是EAX反转)。
  6. 显示新的EAX(将EAX转换为字符串并显示消息)。
  7. 这就是:

    .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 BY USER.
           db 11 dup (?) ;CHARACTERS ENTERED BY USER. 
    
    .code          
    start:
    ;INITIALIZE DATA SEGMENT.
      mov  ax, @data
      mov  ds, ax
    
    ;CONVERT EAX TO STRING TO DISPLAY IT.
      call dollars  ;NECESSARY TO 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 TO STRING).
      mov  ah, 9
      mov  dx, offset buf
      int  21h  
    
    ;FLIP EAX.
      call dollars  ;NECESSARY TO 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 TO STRING).
      mov  ah, 9
      mov  dx, offset buf
      int  21h  
    
    ;CONVERT STRING TO NUMBER (FLIPPED EAX TO EAX).
      mov  si, offset buf  ;STRING TO REVERSE.
      call string2number   ;RETURN IN EBX.
      mov  eax, ebx        ;THIS IS THE NEW EAX FLIPPED.
    
    ;CONVERT EAX TO STRING TO DISPLAY IT.
      call dollars  ;NECESSARY TO DISPLAY.
      call number2string  ;PARAMETER:EAX. RETURN:VARIABLE BUF.
    
    ;DISPLAY 'NEW EAX'.
      mov  ah, 9
      mov  dx, offset msj3
      int  21h  
    
    ;DISPLAY BUF (EAX CONVERTED TO STRING).
      mov  ah, 9
      mov  dx, offset buf
      int  21h  
    
    ;WAIT UNTIL USER PRESS ANY KEY.
      mov  ah, 7
      int  21h
    
    ;FINISH PROGRAM.
      mov  ax, 4c00h
      int  21h           
    
    ;------------------------------------------
    
    flip_eax proc
      mov  si, offset buf  ;DIGITS WILL BE STORED IN BUF.
      mov  bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
      mov  cx, 0  ;COUNTER FOR EXTRACTED DIGITS.
    extracting:       
    ;EXTRACT ONE DIGIT.
      mov  edx, 0 ;NECESSARY TO DIVIDE BY EBX.
      div  ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
    ;INSERT DIGIT IN STRING.
      add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
      mov  [ si ], dl
      inc  si
    ;NEXT DIGIT.
      cmp  eax, 0     ;IF NUMBER IS
      jne  extracting ;NOT ZERO, REPEAT.
    
      ret
    flip_eax endp  
    
    ;------------------------------------------
    ;CONVERT STRING TO NUMBER IN EBX.
    ;SI MUST ENTER POINTING TO THE 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 IT WAS OVER '$', NOT OVER THE LAST DIGIT.
    
    ;CONVERT STRING.
      mov  ebx, 0
      mov  ebp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
    repeat:         
    ;CONVERT CHARACTER.                    
      mov  eax, 0 ;NOW EAX==AL.
      mov  al, [ si ] ;CHARACTER TO PROCESS.
      sub  al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
      mul  ebp ;EAX*EBP = EDX:EAX.
      add  ebx, eax ;ADD RESULT TO 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 WE HAVE FINISHED.
      dec  si ;NEXT DIGIT TO PROCESS.
      loop repeat ;CX-1, IF NOT ZERO, REPEAT.
    
      ret 
    string2number endp
    
    ;------------------------------------------
    ;FILLS VARIABLE STR WITH '$'.
    ;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
    ;THE STRING WILL BE 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 TO CONVERT MUST ENTER IN EAX.
    ;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
    ;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
    ;ORDER TO CONSTRUCT STRING (BUF).
    
    number2string proc
      mov  ebx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
      mov  cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
    cycle1:       
      mov  edx, 0 ;NECESSARY TO DIVIDE BY EBX.
      div  ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
      push dx ;PRESERVE DIGIT EXTRACTED (DL) FOR LATER.
      inc  cx  ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
      cmp  eax, 0  ;IF NUMBER IS
      jne  cycle1  ;NOT ZERO, LOOP. 
    ;NOW RETRIEVE PUSHED DIGITS.
      mov  si, offset buf
    cycle2:  
      pop  dx        
      add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
      mov  [ si ], dl
      inc  si
      loop cycle2  
    
      ret
    number2string endp  
    
    end start