[8086汇编]读取无符号整数并打印该整数

使用0ah中断调用号,读取字符串,然后将字符串转换成整数,打印该整数。

; read u int number
; readuint.asm
; masm.exe readuint.asm
; link.exe readuint.obj
; readuint.exe
data segment
buf db 5
db ?
db 5 dup(?)
number dw 0
data ends

code segment
	assume ds:data, cs:code
main:
	call ReadUInt
	mov [number], ax
	mov dl, 13   ; 打印换行
	mov ah, 02h
	int 21h
	mov dl, 0ah
	mov ah, 02h
	int 21h
	mov ax, [number]
	call print_int
	mov ah, 4ch
	int 21h
ReadUInt proc
	pushf
	push bx
	push cx
	push dx
	push si
	push di
	
	lea dx, buf  ; 读取字符串
	mov ah, 0ah
	int 21h
	
	mov cl, [buf+1]  ; 获取字符串长度
	mov ch, 0
	lea si, [buf+2]  ; 获取字符串地址
	mov ax, 0
	mov di, 0
ReadUInt_loop1:
	dec cx
	cmp cx, -1
	jz ReadUInt_loop1_end
	lea si, [buf+2]
	add si, di     ; 获取当前处理的字符地址
	inc di
	mov bl, [si]   ; 获取字符
	sub bl, 48     ; 字符减去'0'
	mov dl, 10
	mul dl         ; 乘以10
	mov bh, 0
	add ax, bx     ; 加上数字
	jmp ReadUInt_loop1
ReadUInt_loop1_end:
	
	pop di
	pop si
	pop dx
	pop cx
	pop bx
	popf
	ret
ReadUInt endp

print_int proc
	pushf            ; save eflags and register
	push bx
	push cx
	push dx
	
	mov cx, 0        ; digits of number
print_int_loop1:
	cmp ax, 0        ; number == 0 ? end
	jz print_int_end1
	mov dx, 0
	mov bx, 10
	div bx           ; 除以10
	push dx          ; 将余数保存到栈中
	inc cx           ; 位数加1
	jmp print_int_loop1
 
print_int_end1:
	cmp cx, 0        ;是否打印完成
	je print_int_end2
	pop dx
	add dl, '0'      ; 将数字加0,打印对应的ascii码
	mov ah, 02h
	int 21h
	dec cx
	jmp print_int_end1
	
print_int_end2:
	mov dl, 13   ; 打印换行
	mov ah, 02h
	int 21h
	
	pop dx
	pop cx
	pop bx
    popf
	ret
print_int endp
code ends
end main

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值