西电-微机原理-第二次上机实验

数码转换实验

实验要求

在这里插入图片描述

实验代码(来源:https://www.doc88.com/p-9743193089901.html

(1)数制转换

assume cs:code

data segment
    est db " Error:Input invalid",0ah,0dh,'$'
data ends

stack segment
    stk db 64 dup (0)
stack ends

code segment
start: mov ax,stack
       mov ss,ax
       mov sp,offset stk
       mov ax,data
       mov ds,ax
rf:    mov cx,5
       mov ax,0
       mov dx,0
       mov si,0
       mov di,0
rr:    mov ah,01h
       int 21h
       cmp al,0dh
       je rend
       cmp al,'q'
       je qt
       cmp al,'Q'
       je qt
       cmp al,'0'
       jl err
       cmp al,'9'
       jg err
       jmp cnt
err:   call perr
cnt:   sub al,'0'
       mov bh,0
       mov bl,al
       mov ax,di
       push bx
       mov bx,10
       mul bx
       pop bx
       add ax,bx
       adc dx,0
       mov si,dx
       mov di,ax
       loop rr
rend:  call newline
       mov bx,si
       call itb
       mov bx,di
       call itb
       call newline
       jmp rf
qt:    mov ax,4c00h
       int 21h
itb:   mov cx,16
rs:    mov ax,bx
       and ax,01h 
       mov dl,al
       add dl,'0'
       push dx
       shr bx,1
       loop rs
       mov cx,16
r:     pop dx
       mov ah,02h
       int 21h
       loop r
       ret
newline: 
       mov ah,02h
       mov dl,0dh
       int 21h
       mov dl,0ah
       int 21h
       ret
perr:
       mov dx,offset est
       mov ah,09h
       int 21h
       jmp rf
       ret

code ends
end start

(2)数字字符计数

assume cs:code

stack segment
    stk db 16 dup (0)
stack ends

code segment
start: mov ax,stack
       mov ss,ax
       mov sp,0
       mov bx,0
rc:    mov ah,01h
       int 21h     
       cmp al,0dh   
       je brk           
       cmp al,'0'
       jl n
       cmp al,'9'
       jg n
       inc bx
n:     jmp rc           
brk:   call show_num
       mov ax,4c00h
       int 21h
show_num:
       mov  si,0
rs:    mov  ax,bx
       mov  cl,10
       div  cl      ;ah:al div cl ah存放余数 al存放商 
       add  ah,'0'  ;add ah 30h
       mov  dl,ah
       push dx      ;由低位到高位压入栈中
       mov  ah,0    ;ah清零    
       inc  si
       cmp  ax,0    
       je   rt
       jmp  rs      ;被除数不为0则继续
rt:    mov cx,si    ;位数
s:     pop dx
       mov ah,02h
       int 21h
       loop s
       ret

code ends
end start

实验结果

(1)数制转换

在这里插入图片描述

(2)数字字符计数

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yyyyypppppzzzzz/article/details/121589661