8080汇编中关于打印只有打印字符和字符串,没有打印整数的,因此写了一个打印整数的函数print_int,参数存放在寄存器ax中。在dosbox中模拟通过。
; test.asm, run in dosbox
; assembly: masm test.asm
; link : link test.obj
; run : test.exe
data segment
data ends
code segment
assume ds:data,cs:code
main:
mov ax, data
mov ds, ax
mov ax, 231
call print_int
mov ah, 4ch
int 21h
; argument in register ax
print_int:
pushf ; save eflags and register
push bx
push cx
push dx
cmp ax, 0 ; when ax == 0, print 0 directly, then return
jz print_zero
jmp skip_print_zero
print_zero:
mov dl, '0'
mov ah, 02h
int 21h
jmp print_int_end2
skip_print_zero:
mov cx, 0 ; digits of number
print_int_loop1:
cmp ax, 0 ; number == 0 ? end
jz print_int_end1
mov bl, 10
div bl
mov bh, 0
mov bl, ah ; 余数
push bx ; 将余数保存到栈中
mov ah, 0
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
code ends
end main