問題描述
使用 include 'emu8086.inc' 反轉字符串的大小寫和順序 (Reverse case and order of a String using include 'emu8086.inc')
在以下代碼中,我可以反轉字符串,但我不知道如何在此代碼中添加函數以獲得以下結果:
輸入 AbCDeF_XYz 輸出:Zyx_fEdcBa
name "REVERSE"
include "emu8086.inc"
print "Enter a string:"
MOV DX,11
CALL get_string
printn
MOV DI,0x0
ReadString:
MOV AL,[ds+di]
CMP AL,0x0
JE Reverse
INC DI
PUSH AX
JMP ReadString
Reverse:
POP AX
MOV AH,0xE
INT 0x10
CMP AL,0x0
JNE Reverse
HLT
DEFINE_GET_STRING
END
參考解法
方法 1:
I can suggest these 3 solutions for your program:
In your push loop you should also push the zero because now your pop loop will not correctly find a finishing zero!
MOV DI,0x0 ReadString: MOV AL,[ds+di] INC DI PUSH AX cmp al, 0 jne ReadString Reverse:
You can tackle the case swap by looking closely at the input string. It only has the underscore character _ that poses a problem. You'll have to test for it and then bypass the case swap:
MOV DI,0x0 ReadString: MOV AL,[ds+di] INC DI cmp al,"_" je NoSwap xor al,32 ;This toggles lowercase and uppercase NoSwap: PUSH AX cmp al, 0 jne ReadString Reverse:
In your current program you print the terminating zero. This is almost certainly not what you should do!
Reverse: POP AX cmp al,0 je EndOfReverse MOV AH,0xE INT 0x10 Jmp Reverse EndOfReverse: HLT