我刚刚开始学习汇编语言,我试图以相反的顺序打印“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”这是第一个字母,但我希望以相反的顺序得到整个字符串
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:评论每条指令,你会帮自己一个忙。
在这里,尝试一个带循环的程序结构,类似这样......
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,
我建议学习这些东西......
至于这是“低效”,是的,它是。
任何有效的东西比没有的东西效率更高。
同样,这个示例/建议的目的是首先简化逻辑和代码结构,以便您可以在脑海中获得更清晰的图像;然后你可以“优化”
下面是一个简单的反转字符串的方法:
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