如何用汇编语言减去两个数字

时间:2016-05-08 23:21:23

标签: assembly

.model small      

.data               ;data section
  message db  " Enter a number $"
  message2 db " Enter another number $"
  message3 db "  + $"
  message4 db "  = $"
.code
    main proc

       mov ax, seg message  ;3shan minf3sh nn2l el seg msg direct
       mov ds, ax           ;el ds first part mn l string
       mov dx, offset message   ;
       mov ah, 9h
       int 21h      ;3shan y-do it , le print l strings on the screen

       mov ah, 1h  ;read character wl input stored in al
       int 21h                                            

       mov bl, al


       mov ah, 0Eh       ;print new line sequence
       mov al, 0Dh
       int 10h
       mov al, 0Ah
       int 10h

       mov ax, seg message2     ;print msg 2
       mov ds, ax
       mov dx, offset message2
       mov ah,9h
       int 21h 

       mov ah, 1h
       int 21h  

       mov cl, al   

       mov ah, 0Eh       ;print new line sequence
       mov al, 0Dh
       int 10h
       mov al, 0Ah
       int 10h

       mov dl, bl
       mov ah, 2h  
       int 21h 


       mov ax, seg message3
       mov ds, ax
       mov dx, offset message3
       mov ah,9h
       int 21h 

       mov dl, cl
       mov ah, 2h 
       int 21h 

       mov ax, seg message4
       mov ds, ax
       mov dx, offset message4
       mov ah,9h
       int 21h

       sub bl, 30h       ;3shan byd5al l 7aga int fa 3yzen n7wlo le
       sub cl, 30h       ;decimal b3den nrg3o int tani


       sub bl, cl       

       add bl, 30h      ;rg3nah int xD

       mov dl, bl      ;bl 3shan hya ely fiha el sum
       mov ah, 2h
       int 21h
    endp        ;end main


end main        ;end the program

这是从用户中减去2个数字的代码。我试图添加乘法并为它创建一个循环但我每次都失败了,我该怎么办?我自学这种语言。

1 个答案:

答案 0 :(得分:1)

你的程序看起来很好,只是它有一个异常。您的 message3 会显示加号符号,程序实际执行减法操作!不太合乎逻辑,你不同意吗?

要快速更改此程序,以便执行乘法,将sub bl, cl更改为以下内容:

mov al, bl  ;BL is 1st number
mul cl      ;Multiply with 2nd number, product is in AX
mov bl, al  ;Only use the low byte in AL

请注意,该产品必须在此基本程序中产生1位数的结果!

  • 有效选择包括:0 x 7,3 x 2,9 x 1,...
  • 无效选项包括:5 x 4,2 x 7,8 x 9,...