0

所以我写了一个小程序,把一个大于 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
4

1 回答 1

0

您的代码中有 2 个错误。

第一个是你dx之前不归零div cx

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 cxdx未初始化。下一次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

您没有为 and 设置任何有意义的值blbh即使它们是 , 的输入寄存器 mov ah,0ehint 10h参见Ralf Brown 的中断列表

INT 10 - VIDEO - TELETYPE OUTPUT
     AH = 0Eh
     AL = character to write
     BH = page number
     BL = foreground color (graphics modes only)

当您bx用作柜台时,您需要将其存储在内部或外部print_char。例如,保存bx在里面print_char

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
    ret
于 2013-03-11T21:30:03.227 回答