我是汇编语言的新手,我在翻转字符串方面遇到了问题。
例如:
原字符串:“ABCD”
逆转后:“DCBA”
我还想将反转的字符串放入我使用的相同变量名中,而不是使用新的变量名。 我想过使用堆栈,这是我写的代码,但我无法弄清楚我的错误在哪里:
IDEAL
MODEL small
STACK 1000h
DATASEG
first db 'ABCD', 0
CODESEG
start:
mov ax, @data
mov ds, ax
mov si, 0
mov di, 0
loop_start:
mov ax, [first+si]
inc si
test ax, ax
jz done1ax
push ax
jmp loop_start
done1ax:
pop ax
mov [first+di], ax
inc di
cmp di, si
JL done1ax
mov dl, 10
mov ah, 2
int 21h
ret
END start
答案 0 :(得分:0)
让我详细说明@Michael的评论:
IDEAL
MODEL small
STACK 1000h
DATASEG
first db 'ABCD', 0
CODESEG
start:
mov ax, @data
mov ds, ax
xor si, si ; mov si, 0
xor di, di ; mov di, 0
loop_start:
mov al, [first+si]
inc si
test al, al
jz done1ax
push ax ; pushes implicit `AL`
jmp loop_start
done1ax:
pop ax ; pops `AL` and `AH`, `AH` isn't used
mov [first+di], al
inc di
cmp di, si
jb done1ax ; jl is signed comparison: 8000h < 7FFFh
mov ah, 40h ; WriteFile
mov bx, 1 ; STDOUT
mov cx, 4 ; Number of bytes to write
mov dx, OFFSET first ; Data to write
int 21h
mov ax, 4C00h ; Return 0
int 21h
END start