汇编两个十进制数相乘,以二进制和十六进制分别输出

实验7:数值运算与数据输出

1、实验目的:掌握通过键盘输入接收字符串、乘法运算、2/16进制数与ASCII码之间相互转换以及数据显示输出的程序设计方法。
2、实验内容:从键盘输入2个0–99之间的整数,对其做乘法运算,在屏幕上显示出该乘积对应的二进制和十六进制数。
3、实验要求
(1)在屏幕提示:“please input the first number(0–99): ”后,输入一个0–99之间的整数。
若所输入的数不在此范围内,或输入其它字符,则屏幕提示:“input an invalid number, input again!”;
(2)在屏幕提示:“please input the second number(0–99): ”后,输入一个0–99之间的整数。
若所输入的数不在此范围内,或输入其它字符,则屏幕提示:“input an invalid number, input again!”。
(3)对2个整数做乘法运算。
(4)在屏幕上显示出乘积所对应的二进制和十六进制数;
(5)显示部分功能要求用子程序实现。

直接上代码了

DATAS SEGMENT
    string1 db 0aH,'please input the first number(0-99):',0ah,'$'
    string2 db 0ah,'please input the second number(0-99):',0ah,'$'
    string3 db 0ah,'input an invalid number,input again:',0ah,'$'
    x1 db 8,?,8 dup(?)
    x2 db 8,?,8 dup(?)
    num1 dw ?
    num2 dw ?
    num  dw ?
    table DB 30H,31H,32H,33H,34H,35H,36H,37H,38H,39H
          DB 41H,42H,43H,44H,45H,46H,47H,48H,49H,4AH,4BH,4CH,4DH,4EH,4FH,50H,51H,52H
          DB 53H,54H,55H,56H,57H,58H,59H,5AH 
    ;此处输入数据段代码  
DATAS ENDS

STACKS SEGMENT
    db 8 dup(?)
    ;此处输入堆栈段代码
STACKS ENDS

CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
    MOV AX,DATAS
    MOV DS,AX
    mov dx,offset string1
    mov ah,09h
    int 21h
    ;input the number1
    mov  dx,offset x1
    mov  ah,0ah
    int  21h
    jmp  compire1
again1:
    lea dx,string3
    mov ah,09h
    int 21h
    mov  dx,offset x1
    mov  al,00h
    mov  x1[4],al
    mov  ah,0ah
    int  21h
compire1:
    ;inspect the number1
    xor al,al
    mov al,x1[2]
    cmp al,'-'
    jz  again1
    cmp al,'9'
    jnbe again1
    mov al,x1[3]
    cmp al,'9'
    jnbe again1
    mov al,x1[4]
    cmp al,'0'
    jnb again1
    ;show the second tip
    mov dx,offset string2
    mov ah,09h
    int 21h
    ;input the numebr2
    mov  dx,offset x2
    mov  ah,0ah
    int  21h
    jmp  compire2
again2:
    lea dx,string3
    mov ah,09
    int 21h
    mov  dx,offset x2
    mov  al,00h
    mov  x2[4],al
    mov  ah,0ah
    int  21h
compire2:
    xor al,al
    mov al,x2[2]
    cmp al,'-'
    jz  again2
    cmp al,'9'
    jnbe again2
    mov al,x2[3]
    cmp al,'9'
    jnbe again2
    mov al,x2[4]
    cmp al,'0'
    jnb again2
    ;begin to mul the two numbers
    xor ax,ax
    mov bl,10
    mov al,x1[2]
    sub al,30h
    mov cl,x1[3]
    cmp cl,0dh
    jz  non1_
    ;cmp cl,0
    ;jnz sub1
    ;jz  non1
sub1:
    mul bl
    sub cl,30h
    jmp non1
non1_:
    mov cl,0
non1:
    add al,cl
    mov num1,ax
    xor ax,ax
    mov al,x2[2]
    sub al,30h
    mov cl,x2[3]
    cmp cl,0dh
    jz  non2_
    ;cmp cl,0
    ;jnz sub2    
    ;jz  non2
sub2:
    mul bl
    sub cl,30h
    jmp non2
non2_:
    mov cl,0
non2:
    add al,cl
    mov num2,ax
    
    xor bx,bx
    xor ax,ax
    mov ax,num1
    mov bx,num2
    mul bx
    mov num,ax
    mov bx,offset table
    call print_H
    call print_B
    ;此处输入代码段代码
    MOV AH,4CH
    INT 21H
print_H proc near
    push ax
    push cx
    push dx
    mov dl,0ah
    mov ah,02h
    int 21h
    mov ax,num
    xor cx,cx
    mov cl,4
    mov al,ah
    shr al,cl
    
    xlat
    mov dl,al
    mov ah,02h
    int 21h
    mov ax,num
    mov al,ah
    and al,0fh
    xlat
    mov dl,al
    mov ah,02h
    int 21h
    mov ax,num
    shr al,cl
    xlat
    mov dl,al
    mov ah,02h
    int 21h
    mov ax,num
    and al,0fh
    xlat
    mov dl,al
    mov ah,02h
    int 21h
    mov dl,'H'
    int 21h
    mov dl,0ah
    int 21h
    pop dx
    pop cx
    pop ax
    ret
print_H endp

print_B proc near
    push ax
    push bx
    push cx
    push dx
    xor bx,bx
    mov ax,num
    mov bl,ah
    mov cx,8
s1:
    mov dl,30h
    shl bl,1
    adc dl,0
    mov ah,02h
    int 21h
    loop s1
    
    mov ax,num
    mov bl,al
    mov cx,8
s2:
    mov dl,30h
    shl bl,1
    adc dl,0
    mov ah,02h
    int 21h
    loop s2
    mov dl,'B'
    int 21h 
    pop ax
    pop bx
    pop cx
    pop dx
    ret
print_B endp
CODES ENDS
    END START

你可能感兴趣的:(汇编学习笔记)