使用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