Я пытаюсь завершить свое последнее лабораторное упражнение для курса по микропроцессорам и действительно могу помочь с этим. Работа состоит в том, чтобы заполнить закомментированные пустые строки моим собственным кодом. Вот задача:
**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, в которой есть некоторая документация в Интернете. Любая помощь будет высоко ценится! Спасибо.