所以我写了一个小程序,把一个大于9的数字写到屏幕上。但是一旦我把一个值推入栈,我就不能打印任何东西,直到栈是空的。有没有办法绕过这个问题?
代码如下:
print_int: ; Breaks number in ax register to print it
mov cx, 10
mov bx, 0
break_num_to_pics:
cmp ax, 0
je print_all_pics
div cx
push dx
inc bx
jmp break_num_to_pics
print_all_pics:
cmp bx, 0 ; tests if bx is already null
je f_end
pop ax
add ax, 30h
call print_char
dec bx
jmp print_all_pics
print_char: ; Function to print single character to screen
mov ah, 0eh ; Prepare registers to print the character
int 10h ; Call BIOS interrupt
ret
f_end: ; Return back upon function completion
ret发布于 2013-03-12 05:30:03
你的代码中有两个bug。
第一个问题是在div cx之前不会将dx置零
print_int: ; Breaks number in ax register to print it
mov cx, 10
mov bx, 0
break_num_to_pics:
cmp ax, 0
je print_all_pics
; here you need to zero dx, eg. xor dx,dx
xor dx,dx ; add this line to your code.
div cx ; dx:ax/cx, quotient in ax, remainder in dx.
push dx ; push remainder into stack.
inc bx ; increment counter.
jmp break_num_to_pics问题是在除法之前不能将dx置零。第一次div cx时,dx未初始化。下一次到达div cx时,remainder:quotient会被10除以,这没有多大意义。
另一个在这里:
print_char: ; Function to print single character to screen
mov ah, 0eh ; Prepare registers to print the character
int 10h ; Call BIOS interrupt
ret您不必为bl和bh设置任何有意义的值,即使它们是mov ah,0eh、int 10h的输入寄存器(请参见Ralf Brown's Interrupt List)
INT 10 - VIDEO - TELETYPE OUTPUT
AH = 0Eh
AL = character to write
BH = page number
BL = foreground color (graphics modes only)当您使用bx作为计数器时,您需要将其存储在print_char内部或外部。例如,在print_char中保存bx
print_char: ; Function to print single character to screen
mov ah, 0eh ; Prepare registers to print the character
push bx ; store bx into stack
xor bh,bh ; page number <- check this, if I remember correctly 0
; is the default page.
mov bl,0fh ; foreground color (graphics modes only)
int 10h ; Call BIOS interrupt
pop bx ; pop bx from stack
rethttps://stackoverflow.com/questions/15348376
复制相似问题