如何在.COM可执行文件中以相反的顺序打印字符串?

时间:2014-10-14 19:15:17

标签: string assembly dos reverse x86-16

我刚刚开始学习汇编语言,我试图以相反的顺序打印“hello world”,这意味着“dlrow olleh”。问题是我只得到第一个字母作为输出而且订单仍然相同没有变化一点儿!作为一个新手,很多事情对我来说都是未知的,而且我犯了很多错误,由于缺乏知识,我无法识别它们。所以,任何有正确解释的答案都会受到赞赏!这是我的代码:

name "hi" ; can anybody explain what is the use of this?

org 100h

jmp start       ; jump over data declaration

msg    db      "1Hello, World!",0;last character           
msg1   db      "1"

Mov   SI,13;lenght of the string
start: 

Mov  AL,msg[SI]
DEC SI 

Mov  ah ,0eh
int 10h   
mov BL,msg1

CMP msg[SI],BL;comparing to get the end of the string
je stop

jmp start                     

stop:
mov     ah, 0 
int     16h      ; wait for any key....
ret ; return to operating system.

我只输出“1”这是第一个字母,但我希望以相反的顺序得到整个字符串

3 个答案:

答案 0 :(得分:2)

jmp start       ; jump over data declaratio
...
Mov   SI,13;lenght of the string
start: 

这是问题 - 你没有初始化注册si

你需要使用类似的东西:

jmp init       ; jump over data declaratio
...
init:
Mov   SI,13;lenght of the string
start: 

答案 1 :(得分:1)

NOT 针对机器效率进行了优化。

它旨在简化您的问题,以便您更好地了解逻辑。

项目#1:评论每条指令,你会帮自己一个忙。

在这里,尝试一个带循环的程序结构,类似这样......

         Lea     Si, msg                 ;The target string
         Mov     Cx, 13                  ;The length of the string (be careful, but it'll probably work)
         Mov     Bx, Cx                  ;Think: the "base" can actually be an "offset into"

 The_Backwards_Loop:

         Mov     AL,[Bx+Si]              ;Get the "BXth" character in the string
         Mov     AH,0Eh                  ;Code for Bios to put that out to the screen
         Int     10h                     ;Bios puts char in AL onto the screen

         Dec     Bx                      ;Bx was at the Nth character, is now at N-1
         Loop    The_Backwards_Loop      ;And away we go through the rest of the string
                                         ;Note that Cx will dec just like we did Bx


         Mov     Ah, 0                   ;Code to wait for a keystroke
         Int     16h                     ;OS will now wait

         Ret                             ;And we are done, messing up Si, Bx, Ax, Cx, 

我建议学习这些东西......

  • 使用单步执行调试
  • 你会理解你的逻辑
  • 此时,请考虑“效率”
  • 一次尝试任何优化

至于这是“效率低下”,是的,是的。

任何有效的方法都比没有方法更有效。

同样,这个示例/建议的目的是首先简化逻辑和代码结构,以便您可以在脑海中获得更清晰的画面;然后你可以“优化”

答案 2 :(得分:0)

反转字符串的简单方法如下:

INCLUDE 'EMU8086.INC'
.MODEL SMALL
.STACK
.DATA
   ARR DB 10 DUB (?)   
   ;STR DB 0AH,0DH,'OUTPUT: $' 

.CODE
 MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    XOR BX, BX
    MOV CX, 0

  FOR: 
      MOV AH, 1
      INT 21H  

      CMP AL,0DH
      JE  PRINT   

      MOV ARR[BX], AL
      INC BX 
      INC CX
   JMP FOR


 PRINT:  
    MOV AH, 2       ;new line  
    MOV DL, 0AH
    INT 21H                 
    MOV DL, 0DH     ;curage return
    INT 21H   


  BACK: 
     CMP CX, 0
     JE FINISH
     DEC CX
     DEC BX  

     MOV AL, ARR[BX]

     MOV AH, 2       
     MOV DL, AL  
     INT 21H            
   JMP BACK

 FINISH:
    MOV AH,4CH
    INT 21H   

 MAIN ENDP
相关问题