汇编语言emu8086的回文程序 [英] Palindrome program for emu8086 in assembly language

查看:69
本文介绍了汇编语言emu8086的回文程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完成我的微处理器课程的最后一个实验室练习,并且真的可以在这方面得到一些帮助.工作是用我自己的代码填充注释的空行.任务如下:

**Task 2. 测试字符串是否为回文**修改前面的程序,使其检查字符串是否为回文.补充以下程序.添加缺少的说明.包括emu8086.inc";程序开始组织 100 小时jmp开始;内存变量:msg3 db 0ah,0dh,"字符串是回文.",0msg2 db 0ah,0dh,"字符串不是回文.",0msg1 db "输入一个字符串(最多 128 个字符):",0;保存输入字符串的缓冲区mystr db 128 dup (0),0mystrREV db 128 dup (0),0endl db 0dh,0ah,0长度 db 0开始: lea SI, msg1 ;留言地址呼叫 PRINT_STRING ;打印来自 [SI] 的消息;字符串 mystring:在此处读取字符串!;字符串最大值长度;将字符串读入 [DI]lea si,endl调用 print_string;将缓冲区 mystr 中的字符数计算到 CX 中:mov cl,0 ;从 0 开始lea SI,mystr ;将 SI 指向 mystr苔丝:mov al,[SI],0 ;数据结束?cmp al,0 ;--你好;继续下一步包括 ;增量数据计数器公司 SI ;增量数据指针jmp苔丝;检查下一个;以相反的顺序将 mystr 复制到 mystrREV 中seur: mov 长度,cl ;以长度存储 # 个字符;将结果缓冲区地址写入 DI;源缓冲区地址 id SI(递减)合作社:;从源复制字符;复制字符到目的地;递减源指针;递增结果指针;递减计数器;如果没有完成,请下一步;打印两个缓冲区lea si,mystr调用 print_string ;打印 mystrlea si,endl调用 print_string ;打印 cr+lflea si,mystrREV调用 print_string ;打印 mystrREVlea si,endl调用print_string ;打印cr+lf;比较字符串.如果相等=>回文mov cl,length ;# 缓冲区中的字符数lea si,mystr ;第一个缓冲区的地址lea di,mystrREV ;第二个缓冲区的地址niis: cmp cl,0 ;测试是否比较结束/缓冲;跳转到确定,回文/空缓冲区;源缓冲区地址;结果缓冲区地址;都一样,还有机会吗?;Nop,跳转到打印 NOT-message 并退出: 增加源指针;增加目标指针;递减计数器jmp niis ;下一个试试正: lea SI,msg3 ;是的,回文调用 PRINT_STRING ;打印出来jmp 波特;然后退出负数:lea si,msg2 ;不是回文调用 PRINT_STRING ;打印出来退出bort: mov ax,4c00h ;返回 ms-dos 的代码整数 21 小时;调用 ms-dos 终止程序回复;宏定义DEFINE_GET_STRINGDEFINE_PRINT_STRINGDEFINE_PRINT_NUMDEFINE_PRINT_NUM_UNSend ;程序结束

我的程序只将输入字符串的第一个字母打印为反转字符串,并没有真正正确测试回文.这是我到目前为止所做的:

 包含emu8086.inc";程序开始组织 100 小时jmp开始;内存变量:msg3 db 0ah,0dh,"字符串是回文.",0msg2 db 0ah,0dh,"字符串不是回文.",0msg1 db "输入一个字符串(最多 128 个字符):",0;保存输入字符串的缓冲区mystr db 128 dup (0),0mystrREV db 128 dup (0),0endl db 0dh,0ah,0长度 db 0开始: lea SI, msg1 ;消息msg1地址呼叫 PRINT_STRING ;打印来自 [SI] 的消息;************************ 我的代码开始 ********************lea di, mystr ;字符串 mystring:在此处读取字符串!移动 dx, 128 ;字符串最大值长度调用 get_string ;将字符串读入 [DI];************************ 我的代码结束 ************************lea si,endl ;字符串结尾调用 print_string ;打印结束;将缓冲区 mystr 中的字符数计算到 CX 中:mov cl,0 ;从 0 开始lea SI,mystr ;将 SI 指向 mystr苔丝:mov al,[SI],0 ;数据结束?cmp al,0 ;——"——你好;继续下一步包括 ;增量数据计数器公司 SI ;增量数据指针jmp苔丝;检查下一个;以相反的顺序将 mystr 复制到 mystrREV 中seur: mov 长度,cl ;以长度存储 # 个字符;************************ 我的代码开始 ********************;此代码块出现问题lea di, mystrREV ;将结果缓冲区地址写入 DIlea si, mystr ;源缓冲区地址 id SI(递减)coop:mov al, [si] ;从源复制字符mov [di], al ;复制字符到目的地十二月;递减源指针公司 迪;递增结果指针dec ;递减计数器cmp cl,0 ;如果没有完成,请下一步珍妮鸡舍;************************ 我的代码结束 ************************;打印两个缓冲区lea si,mystr调用 print_string ;打印 mystrlea si,endl调用 print_string ;打印 cr+lflea si,mystrREV调用 print_string ;打印 mystrREVlea si,endl ;代码打印不足调用print_string ;打印cr+lf;比较字符串.如果相等=>回文mov cl,length ;# 缓冲区中的字符数lea si,mystr ;第一个缓冲区的地址lea di,mystrREV ;第二个缓冲区的地址niis: cmp cl,0 ;测试是否比较结束/缓冲;************************ 我的代码开始 ********************je 阳性 ;跳转到确定,回文/空缓冲区lea si,mystr ;源缓冲区地址lea di,mystrREV ;结果缓冲区地址cmp di,si ;都一样,还有机会吗?jne 否定的;Nop,跳转到打印 NOT-message 并退出公司 ;增加源指针公司 迪;增加目标指针dec ;递减计数器;************************ 我的代码结束 ************************jmp niis ;下一个试试正: lea si,msg3 ;是的,回文调用 PRINT_STRING ;打印出来jmp 波特;然后退出负数:lea si,msg2 ;不是回文调用 PRINT_STRING ;打印出来退出bort: mov ax,4c00h ;返回 ms-dos 的代码整数 21 小时;调用 ms-dos 终止程序回复;宏定义DEFINE_GET_STRINGDEFINE_PRINT_STRINGDEFINE_PRINT_NUMDEFINE_PRINT_NUM_UNSend ;程序结束

我的结果:

输入一个字符串(最多 128 个字符):abba阿爸一种字符串不是回文.

控制台视图

预期结果:

输入一个字符串(最多 128 个字符):innostunutsonniinnostunutsonniinnostunutsonni字符串是回文.

控制台视图

我们使用的是名为 emu8086 的旧模拟器软件,该软件在线提供了一些文档.任何帮助将不胜感激!谢谢.

解决方案

问题 1(改进)

<块引用>

<代码>;************************ 我的代码开始 ********************;此代码块出现问题lea di, mystrREV ;将结果缓冲区地址写入 DIlea si, mystr ;源缓冲区地址 id SI(递减)合作社:mov al, [si] ;从源复制字符mov [di], al ;复制字符到目的地十二月;递减源指针公司 迪;递增结果指针dec ;递减计数器cmp cl,0 ;如果没有完成,请下一步珍妮鸡舍;************************ 我的代码结束 ************************

您应该在注释; Source buffer address id SI(decremented)"中得到提示.

为了向后遍历源字符串——这就是递减"的意思——你需要将源指针SI初始化到字符串的末尾.这意味着您需要计算 StartOfString + LengthOfString - 1.

<代码>;************************ 我的代码开始 ********************lea di, mystrREV ;将结果缓冲区地址写入 DIlea bx, mystr ;源缓冲区地址 id SI(递减)添加 bl, cladc bh, 0lea si, [bx-1]合作社:mov al, [si] ;从源复制字符mov [di], al ;复制字符到目的地十二月;递减源指针公司 迪;递增结果指针dec ;递减计数器jne coop ;如果没有完成,请下一步;************************ 我的代码结束 ************************

请注意,您不需要 cmp cl,0 指令,因为前面的 dec cl 指令已经设置了必要的标志.

问题 2(新)

<块引用>

<代码>;比较字符串.如果相等=>回文mov cl,length ;# 缓冲区中的字符数lea si,mystr ;第一个缓冲区的地址lea di,mystrREV ;第二个缓冲区的地址尼斯:cmp cl,0 ;测试是否比较结束/缓冲;************************ 我的代码开始 ********************je 阳性 ;跳转到确定,回文/空缓冲区lea si,mystr ;源缓冲区地址lea di,mystrREV ;结果缓冲区地址cmp di,si ;都一样,还有机会吗?jne 否定的;Nop,跳转到打印 NOT-message 并退出公司 ;增加源指针公司 迪;增加目标指针dec ;递减计数器;************************ 我的代码结束 ************************

您用于比较字符串的代码根本无法比较!他们给出的评论具有误导性.

您不希望 SIDI 中的地址再次出现.您需要获取该寄存器指向的字符,然后进行比较:

<代码>;************************ 我的代码开始 ********************je 阳性mov al, [si] ;源缓冲区地址<<<<<<<误导性评论mov dl, [di] ;结果缓冲区地址<<<<<<<<误导性评论cmp al, dl ;都一样,还有机会吗?负

I'm trying to complete my last lab exercise for my microprocessors course and could really use some help with this. The job is to fill the commented empty lines with my own code. Here's the task:

**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

My program only prints the first letter of the input string as reversed string and doesn't really test the palindrome properly. This is what I've done so far:

            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

My result:

Enter a string (max 128 characters): abba
abba
a

The string is NOT a palindrome.

Console view

Expected result:

Enter a string (max 128 characters): innostunutsonni
innostunutsonni
innostunutsonni

The string is a palindrome.

Console view

We're using this old emulator software called emu8086 which has some documentation online. Any help would be highly appreciated! Thank you.

解决方案

Problem 1 (improved)

; *********************** 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 **********************

You should have taken the hint in the comment "; Source buffer address id SI(decremented)".

In order to traverse the source string backwards - that's what 'decremented' means - you need to initialize the source pointer SI to the end of the string. That means that you need to compute StartOfString + LengthOfString - 1.

; *********************** My code starts *********************  
lea di, mystrREV    ; Result buffer address into DI
lea bx, mystr       ; Source buffer address id SI(decremented)
add bl, cl
adc bh, 0
lea si, [bx-1]
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
jne coop            ; Take next if not done                                           
; *********************** My code ends **********************

Please notice that you don't need that cmp cl,0 instruction because the preceding dec cl instruction already set the necessary flags.

Problem 2 (new)

; 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 ***********************

Your code to compare the strings does not compare at all! The comments they gave are misleading.

You don't want the addresses again in SI and DI. You need to fetch the characters that this registers point at and then compare those:

; *********************** My code starts ********************* 
je      positive   
mov     al, [si]        ; Source buffer address   <<<<< misleading comment
mov     dl, [di]        ; Result buffer address   <<<<< misleading comment
cmp     al, dl          ; Are same, still chance?
jne     negative

这篇关于汇编语言emu8086的回文程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆