assembly 如何使用TASM执行数据移动和算术指令?

4ktjp1zp  于 2023-05-13  发布在  其他
关注(0)|答案(2)|浏览(197)

我们目前在汇编编程,我们的任务是使用TASM执行基本数据移动和算术指令。在本练习中,我们应该创建一个程序来实现这些条件:
a.输入必须是一位整数
B.两个输入之和必须小于10
c.两个输入之差必须小于10但大于0。
d.必须在输入两个后或显示答案前询问命令
其示例输出将是:

Enter first integer: 7  
Enter second integer: 2_  
Sum: 9  
Difference: 5

我们尝试了这个代码:

.model small
.stack 100h

.data
msg1 db 'Enter first integer: $'
msg2 db 'Enter second integer: $'
msg3 db 'Sum: $'
msg4 db 'Difference: $'
newline db 0dh, 0ah, '$'
max_sum equ 10
max_diff equ 10

.code
main proc
    mov ax, @data
    mov ds, ax

    ; ask for first input
    mov ah, 9
    lea dx, msg1
    int 21h

    ; read first input
    mov ah, 1
    int 21h
    sub al, 30h   
    mov bl, al    ; save first input in BL

    ; ask for second input
    mov ah, 9
    lea dx, msg2
    int 21h

    ; read second input
    mov ah, 1
    int 21h
    sub al, 30h   
    mov cl, al    ; save second input in CL

    ; calculate sum
    add bl, cl
    cmp bl, max_sum
    jae error     ; if sum>10

    ; calculate difference
    mov al, bl
    sub al, cl
    cmp al, max_diff
    jae error     ; if diff > || < 10

    ; display sum
    mov ah, 9
    lea dx, msg3
    int 21h
    mov dl, bl
    add dl, 30h  
    mov ah, 2
    int 21h

    ; display difference
mov ah, 9
lea dx, newline
int 21h
mov ah, 9
lea dx, msg4
int 21h
mov dl, al
add dl, 30h   
mov ah, 2
int 21h

; ask for <ENTER> command
mov ah, 9
lea dx, newline
int 21h

mov ah, 4ch
int 21h

error:
    ; display error message
    mov ah, 9
    lea dx, newline
    int 21h
    mov ah, 9
    lea dx, msg1
    int 21h
    jmp main     ; repeat input process

main endp

end main

运行它后,它工作正常,但问题是输出出来的错误,例如在差异。你们能教我们怎么解决吗我们都是这方面的初学者,这是在我们没有事先知识的情况下给我们的。

llmtgqce

llmtgqce1#

; calculate sum
add bl, cl
cmp bl, max_sum
jae error     ; if sum>10

由于需要使用BL和CL输入两次,一次用于和,一次用于差,因此不能将和存储在BL寄存器中。您可以将总和存储在基于内存的命名变量中,将其存储在像BH这样的空闲寄存器中,或者将其推送到堆栈上。为了快速修复,我选择BH:

; calculate sum
mov  bh, bl
add  bh, cl
cmp  bh, max_sum
jae  error     ; if sum is 10 or more
; calculate difference
mov al, bl
sub al, cl
cmp al, max_diff
jae error     ; if diff > || < 10

你不能在AL寄存器中留下差异,因为你接下来要使用的DOS函数(DOS.PrintChar函数02 h和DOS.PrintString函数09 h)都会破坏AL。有趣的是,我在几天前写的一个答案中也犯了类似的错误。让我们用CH来表示差异。
现在,仅与MAX_DIFF进行比较并在AboveOrEqual上跳转,只能部分地遵守任务要求!大于9的差值和小于0的差值将被拒绝,但是0本身(BL == CL)呢?

; calculate difference
mov  ch, bl
sub  ch, cl
jle  error     ; if diff is 0 or less
cmp  ch, max_diff
jge  error     ; if diff is 10 or more
; ask for <ENTER> command
mov ah, 9
lea dx, newline
int 21h
mov ah, 4ch
int 21h

这段代码并不执行注解中所说的操作。无论如何,该任务要求您在显示结果之前等待ENTER**。这是在退出程序前的画面。

mov ah, 9
lea dx, msg1
int 21h
jmp main     ; repeat input process

消息 * msg 1 * 有重复的风险。在跳转到 main 之前,您不应该打印第一条消息,因为它几乎是 main 做的第一件事。

我重写

这还包括适当的换行符,以便输出类似于您所指示的内容。

.model small
.stack 100h

.data
msg1     db 'Enter first integer: $'
msg2     db 13, 10, 'Enter second integer: $'
msg3     db 13, 10, 'Sum: $'
msg4     db 13, 10, 'Difference: $'
newline  db 13, 10, '$'
max_sum  equ 10
max_diff equ 10

.code
main proc
    mov ax, @data
    mov ds, ax

    ; ask for first input
    mov ah, 9
    lea dx, msg1
    int 21h

    ; read first input
    mov ah, 1
    int 21h
    sub al, 30h   
    mov bl, al    ; save first input in BL

    ; ask for second input
    mov ah, 9
    lea dx, msg2
    int 21h

    ; read second input
    mov ah, 1
    int 21h
    sub al, 30h   
    mov cl, al    ; save second input in CL

    ; calculate sum in BH
    mov  bh, bl
    add  bh, cl
    cmp  bh, max_sum
    jae  error     ; if sum is 10 or more

    ; calculate difference in CH
    mov  ch, bl
    sub  ch, cl
    jle  error     ; if diff is 0 or less
    cmp  ch, max_diff
    jge  error     ; if diff is 10 or more

    ***************************************
    HERE YOU NEED TO WAIT FOR THE ENTER KEY
    ***************************************

    ; display sum
    mov  ah, 9
    lea  dx, msg3
    int  21h
    mov  dl, bh        ; BH is sum
    add  dl, 30h  
    mov  ah, 2
    int  21h

    ; display difference
    mov  ah, 9
    lea  dx, msg4
    int  21h
    mov  dl, ch        ; CH is difference
    add  dl, 30h
    mov  ah, 2
    int 21h

    mov  ax, 4C00h
    int  21h

error:
    ; display error message
    mov  ah, 9
    lea  dx, newline
    int  21h
    jmp  main     ; repeat input process

main endp

end main
neekobn8

neekobn82#

msg 3 al显示正确值之前,msg 3 al更改后enter image description here
= 6
这里是修改后的代码;)

.model small
.stack 100h

.data
   msg1 db 'Enter first integer: $'
   msg2 db 'Enter second integer: $'
   msg3 db 'Sum: $'
   msg4 db 'Difference: $'
   msg5 db '*** Error: Sum >= 10. Hint: Int1 + Int2 < 10 $'
   msg6 db '*** Error: Difference = 0. Hint: Int1 != Int2 $'
   newline db 0dh, 0ah, '$'
   max_sum equ 10
   ;max_diff equ 0

.code       
   main proc
   mov ax, @data
   mov ds, ax

   ; ask for first input
   mov ah, 9
   lea dx, msg1
   int 21h

   ; read first input
   mov ah, 1
   int 21h
   sub al, 30h   
   mov bl, al    ; save first input in BL

   mov ah, 9
   lea dx, newline
   int 21h

   ; ask for second input
   mov ah, 9
   lea dx, msg2
   int 21h

   ; read second input
   mov ah, 1
   int 21h
   sub al, 30h   
   mov cl, al    ; save second input in CL

   mov ah, 9
   lea dx, newline
   int 21h

   ; calculate sum
   push bx
   add bl, cl
   cmp bl, max_sum 
   jae error     ; if sum>10
   ; display sum
   mov ah, 9
   lea dx, msg3
   int 21h 
   mov dl, bl
   add dl, 30h  
   mov ah, 2
   int 21h

   pop bx

   ;calculate difference
   cmp bl,cl
   jz DiffIsZero
   ja positive_number
   jb negative_number

   positive_number:    
      mov al, bl      
      sub al, cl 
      jmp ShowDiff
 
   negative_number:    
      mov al, cl      
      sub al, bl  
      jmp ShowDiff

   ShowDiff:

   ; display difference
   push ax
   mov ah, 9
   lea dx, newline
   int 21h

   mov ah, 9
   lea dx, msg4
   int 21h
   pop ax

   mov dl, al
   add dl, 30h   
   mov ah, 2
   int 21h

  ; ask for <ENTER> command
   mov ah, 9
   lea dx, newline
   int 21h

   mov ah,8h     ; wait for key 
   int 21h

   mov ah, 4ch
   int 21h

  error:
     pop bx
     ; display error message sum >= 10
     mov ah, 9
     lea dx, newline
     int 21h

     mov ah, 9
     lea dx, msg5
     int 21h

     mov ah, 9
     lea dx, newline
     int 21h
     mov ah, 9
     lea dx, newline
     int 21h
     jmp main     ; repeat input process

 DiffIsZero:
   ; display error message, difference = 0
   mov ah, 9
   lea dx, newline
   int 21h

   mov ah, 9
   lea dx, newline
   int 21h
   mov ah, 9
   lea dx, msg6
   int 21h

   mov ah, 9
   lea dx, newline
   int 21h
   mov ah, 9
   lea dx, newline
   int 21h
   jmp main     

main endp

end main

相关问题