问题
I am totally new in assembly language programming and I am stuck with a problem in which I need to change case of the entered string plus reverse the string as well. I am using emu8086. I the following code I am either able to change case or reverse the string. but I need to perform both the operations simultaneously.
.MODEL CASECHANGE
.DATA
MSG1 DB 0DH,0AH, 'Enter string:$'
MSG2 DB 0DH,0AH, 'String in reverse case:$'
STR1 DB 255 DUP(?)
.CODE
START:
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
LEA SI,STR1
MOV AH,01H
jz offsets
GET:
INT 21H
MOV BL,AL
CMP AL,0DH
JE SET
XOR AL,20H
MOV [SI],AL
INC SI
JMP GET
SET:
MOV AL,'$'
MOV [SI],AL
LEA DX,MSG2
MOV AH,09H
INT 21H
LEA DX,STR1
MOV AH,09H
INT 21H
MOV AL,09H
JMP START
JMP BACK
.EXIT
below code reverse the string and above code changes case and I need to join both codes to achieve desired output.
BACK:
int 21h
MOV BL,AL
cmp al,0dh
jz exit
mov [si],al
inc si
inc ch
jmp back
EXIT:
lea dx,MSG2
mov ah,09h
int 21h
cmp1:
mov al,[si]
mov dl,al
mov ah,02h
int 21h
dec si
dec ch
jnz cmp1
mov ah,01ch
int 21h
OFFSETS:
mov ch,01h
mov si,offset STR1
END START
These 2 set of codes are provided by my instructor so can only play with this code.
回答1:
I didn't really read your massive wall of uncommented code.
To reverse a buffer in-place, get pointers to the first and last characters, then:
Load the bytes into registers, then store the opposite registers back to the pointers.
Increment the start pointer
si
, decrement the end pointerdi
.loop as long as start < end:
cmp si, di / jb
Downcasing can be done on a single character, so you can do that on both bytes separately, when you have them in registers while you're swapping. Just check that it's between 'A'
and 'Z'
, then add 0x20. (You unfortunately can't just or al, 20H
unless you know that your character is already either a lower or uppercase letter, and not some other ASCII character).
Reversing to a new buffer is even easier. Just go forwards in one array and backwards in the other, for count
bytes.
If your target baseline CPU feature set included 386 instructions, you could have loaded 4B at a time and used bswap
to reverse bytes 4 at a time. Or with SSSE3, pshufb
to reverse 16B at a time.
来源:https://stackoverflow.com/questions/33578121/emu8086-change-case-of-the-entered-string-and-reverse-it