在汇编中反转字符串 (TASM)
reversing a string in assembly (TASM)
我是汇编语言的新手,我在反转字符串时遇到问题。
例如:
原始字符串:"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
让我详细说明@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
我是汇编语言的新手,我在反转字符串时遇到问题。
例如:
原始字符串:"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
让我详细说明@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