从键盘上输入任意两个不大于2位数的正整数,计算其乘积。结果在屏幕上显示。

emu8086汇编语言

从键盘上输入任意两个不大于2位数的正整数,计算其乘积。结果在屏幕上显示。

.

data segment
    string1 db "a=$";
    string2 db "b=$";
    answear db 0,0,0,0,"$";
    ansstring db "a*b=$";
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
   assume cs:code,ds:data;
    mov ax, data;
    mov ds, ax;
    mov dx,offset string1;
    mov ah,9h;
    int 21h;// output"a="
    
    mov ah,1h;
    int 21h;
    sub al,30h;
    mov bl,al;
    mov al,0ah;
    mul bl;
    mov bl,al;//get the tens
    
    mov ah,1h;
    int 21h;
    sub al,30h;
    add bl,al;//get the ones  
    
    mov ah,2;
    mov dl,0ah;
    int 21h;
    mov dl,0dh;
    int 21h;
    
    mov ax,data;
    mov ds,ax;
    mov dx,offset string2;
    mov ah,9h;
    int 21h; //output"b="
    
    mov ah,1h;
    int 21h;
    sub al,30h;
    mov cl,al;
    mov al,0ah;
    mul cl;
    mov cl,al;//get the tens
    
    mov ah,1h;
    int 21h;
    sub al,30h;
    add cl,al;//get the ones  
    
    mov ah,2;
    mov dl,0ah;
    int 21h;
    mov dl,0dh;
    int 21h;
    
    mov al,bl;
    mul cl; //answear in ax
    mov bx,0ah;
    mov cx,4;
    mov si,3; 
    ans:  
    mov dx,0000h;
    div bx;
    add dl,30h;
    mov byte ptr answear+si,dl;
    dec si;
    loop ans;
    
    mov ax,data;
    mov ds,ax;
    mov dx,offset ansstring;
    mov ah,9h;
    int 21h; //output"a*b="
    mov ax,data;
    mov ds,ax;
    mov dx,offset answear;
    mov ah,9h;
    int 21h;
    mov ah,4ch;
    int 21h;
  
ret               
ends
end start ;
  • 键盘读入的数是其ASCII码值,要减30得到真实的数值。
  • 汇编语言除法,无符号数除为DIV SRC:
    字节:(AX)/(SRC)———>(AL)商、(AH)余数
    字型:(DX:AX)/(SRC)——>(AX)商、(DX)余数
 ans:  
    mov dx,0000h;
    div bx;
    add dl,30h;
    mov byte ptr answear+si,dl;
    dec si;
    loop ans;

在这一段中,若div bx;换成div bl;,则会默认为字节除,计算时会发生溢出。

  • 还有,com中会在输出时报错“INT 21h,AH=09h-address:00000 byte 24h not found after 2000 bytes”,换成exe就好了,懵逼脸,也不知道这是为什么。

你可能感兴趣的:(从键盘上输入任意两个不大于2位数的正整数,计算其乘积。结果在屏幕上显示。)