我正在尝试完成我的微处理器类(class)的最后一个实验练习,并且真的需要一些帮助。 工作是用我自己的代码填充注释的空行。 这是任务:
**Task 2. Test if the string is a palindrome**
Modify the previous program so, that it checks whether the string is a palindrome. Complement the following program. Add the missing instructions.
include "emu8086.inc"
; START-OF-PROGRAM
org 100h
jmp start
; Memory variables:
msg3 db 0ah,0dh,"The string is a palindrome.",0
msg2 db 0ah,0dh,"The string is NOT a palindrome.",0
msg1 db "Enter a string (max 128 characters): ",0
; the buffer to save the entered string
mystr db 128 dup (0),0
mystrREV db 128 dup (0),0
endl db 0dh,0ah,0
length db 0
start: lea SI, msg1 ; Message address
CALL PRINT_STRING ; Print message from [SI]
; String mystring: Read string here!
; String max. length
; Read string into [DI]
lea si,endl
call print_string
; count the number of characters in the buffer mystr into CX:
mov cl,0 ; start from 0
lea SI,mystr ; Point SI to mystr
tess: mov al,[SI],0 ; End of data?
cmp al,0 ; -“-
je seur ; Proceed to next step
inc cl ; Increment data counter
inc SI ; Increment data pointer
jmp tess ; Check next
; copy mystr into mystrREV in reverse order
seur: mov length,cl ; Store # of characters in length
; Result buffer address into DI
; Source buffer address id SI(decremented)
coop: ; Copy character from source
; Copy character to destination
; Decrement source pointer
; Increment result pointer
; Decrement counter
; Take next if not done
; print both buffers
lea si,mystr
call print_string ; Print mystr
lea si,endl
call print_string ; Print cr+lf
lea si,mystrREV
call print_string ; Print mystrREV
lea si,endl
call print_string ;print cr+lf
; compare strings. If equal => palindrome
mov cl,length ; # of characters in buffers
lea si,mystr ; address of first buffer
lea di,mystrREV ; address of second buffer
niis: cmp cl,0 ; test if end-of-comparison/buffer
; jump to ok, palindrome/empty buffer
; Source buffer address
; Result buffer address
; Are same, still chance?
; Nop, jump to print NOT-message and exit
: increment source pointer
; increment destination pointer
; decrement counter
jmp niis ; Try next
positive: lea SI,msg3 ; Yess, palindrome
call PRINT_STRING ; Print it
jmp bort ; and exit
negative: lea si,msg2 ; NOT a palindrome
call PRINT_STRING ; Print it and exit
bort: mov ax,4c00h ; code for return to ms-dos
int 21h ; call ms-dos terminate program
ret
; Macro definitions
DEFINE_GET_STRING
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
end ;END-OF-PROGRAM
我的程序只将输入字符串的第一个字母打印为反转字符串,并没有真正正确地测试回文。这是我到目前为止所做的:
include "emu8086.inc"
; START-OF-PROGRAM
org 100h
jmp start
; Memory variables:
msg3 db 0ah,0dh,"The string is a palindrome.",0
msg2 db 0ah,0dh,"The string is NOT a palindrome.",0
msg1 db "Enter a string (max 128 characters): ",0
; The buffer to save the entered string
mystr db 128 dup (0),0
mystrREV db 128 dup (0),0
endl db 0dh,0ah,0
length db 0
start: lea SI, msg1 ; Message msg1 address
CALL PRINT_STRING ; Print message from [SI]
; *********************** My code starts *********************
lea di, mystr ; String mystring: Read string here!
mov dx, 128 ; String max. length
call get_string ; Read string into [DI]
; *********************** My code ends ***********************
lea si,endl ; String endl
call print_string ; Print endl
; count the number of characters in the buffer mystr into CX:
mov cl,0 ; start from 0
lea SI,mystr ; Point SI to mystr
tess: mov al,[SI],0 ; End of data?
cmp al,0 ; -"-
je seur ; Proceed to next step
inc cl ; Increment data counter
inc SI ; Increment data pointer
jmp tess ; Check next
; copy mystr into mystrREV in reverse order
seur: mov length,cl ; Store # of characters in length
; *********************** My code starts *********************
; Something goes wrong in this code block
lea di, mystrREV ; Result buffer address into DI
lea si, mystr ; Source buffer address id SI(decremented)
coop:mov al, [si] ; Copy character from source
mov [di], al ; Copy character to destination
dec si ; Decrement source pointer
inc di ; Increment result pointer
dec cl ; Decrement counter
cmp cl,0 ; Take next if not done
jne coop
; *********************** My code ends ***********************
; print both buffers
lea si,mystr
call print_string ; Print mystr
lea si,endl
call print_string ; Print cr+lf
lea si,mystrREV
call print_string ; Print mystrREV
lea si,endl ; CODE DOESN'T PRINT ENOUGH
call print_string ;print cr+lf
; compare strings. If equal => palindrome
mov cl,length ; # of characters in buffers
lea si,mystr ; address of first buffer
lea di,mystrREV ; address of second buffer
niis: cmp cl,0 ; test if end-of-comparison/buffer
; *********************** My code starts *********************
je positive ; jump to ok, palindrome/empty buffer
lea si,mystr ; Source buffer address
lea di,mystrREV ; Result buffer address
cmp di,si ; Are same, still chance?
jne negative ; Nop, jump to print NOT-message and exit
inc si ; increment source pointer
inc di ; increment destination pointer
dec cl ; decrement counter
; *********************** My code ends ***********************
jmp niis ; Try next
positive: lea si,msg3 ; Yess, palindrome
call PRINT_STRING ; Print it
jmp bort ; and exit
negative: lea si,msg2 ; NOT a palindrome
call PRINT_STRING ; Print it and exit
bort: mov ax,4c00h ; code for return to ms-dos
int 21h ; call ms-dos terminate program
ret
; Macro definitions
DEFINE_GET_STRING
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
end ;END-OF-PROGRAM
我的结果:
Enter a string (max 128 characters): abba
abba
a
The string is NOT a palindrome.
预期结果:
Enter a string (max 128 characters): innostunutsonni
innostunutsonni
innostunutsonni
The string is a palindrome.
我们正在使用这个名为 emu8086 的旧仿真器软件,它有一些在线文档。 任何帮助将不胜感激!谢谢。
最佳答案
您将 SI 设置为指向 mystr
的第一个字节,并在复制该字节后递减 SI,从而将 SI 从缓冲区中取出。 SI 开头应该指向mystr
中的最后一个字符。而不是
; Something goes wrong in this code block
lea di, mystrREV ; Result buffer address into DI
lea si, mystr ; Source buffer address id SI(decremented)
coop:mov al, [si] ; Copy character from source
mov [di], al ; Copy character to destination
dec si ; Decrement source pointer
试试这个:
lea di, mystrREV ; Result buffer address into DI
lea si, mystr ; Source buffer address id SI(decremented)
movzx cx,[length] ; Let cx be the size of mystr.
add si,cx ; Let si point behind mystr.
dec si ; Let si point to the last character of mystr.
coop:mov al, [si] ; Copy character from source
mov [di], al ; Copy character to destination
dec si ; Decrement source pointer
包括 MASM 在内的大多数汇编程序中的指令 LEA 要求(或至少容忍)第二个操作数放在方括号中,例如LEA DI,[mystrREV],你应该习惯这种语法。
指令tess: mov al,[SI],0 ;数据结束?
看起来很奇怪,你确定它不会触发错误吗?
与其在 SO 上发布图片,不如从控制台复制和粘贴文本,这样它就包含在您的问题中,并且即使在 imgur.com 停止存在时也能保持可见。
仅发布源文本是不够的,还应该附上它是如何组合和链接的信息。
关于assembly - emu8086的汇编语言回文程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61442174/