程序集8086中的堆栈出错(实模式)(堆栈已满时打印字符)

时间:2013-03-11 20:52:31

标签: assembly printing x86 stack x86-16

所以我写了一个小程序来写一个高于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

1 个答案:

答案 0 :(得分: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

您没有为blbh设置任何有意义的值,即使这些值是输入寄存器 mov ah,0ehint 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内部或外部。例如,将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