【问题标题】:Error with stack in assembly 8086 (real mode) (printing char while stack full)程序集 8086(实模式)中的堆栈错误(堆栈已满时打印字符)
【发布时间】:2013-02-27 04:58:45
【问题描述】:

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

【问题讨论】:

    标签: assembly printing x86 stack x86-16


    【解决方案1】:

    您的代码中有 2 个错误。

    第一个是你在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 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
    

    【讨论】:

    • 谢谢。今天晚些时候会试试这个。我确实注意到了 dx,一旦我尝试反向打印值(所以 9573 变为 3759,因为这也可以完成我需要 print_int 函数),但是,我不知道 bx 的事情。非常感谢。
    猜你喜欢
    • 2017-08-15
    • 2015-08-05
    • 2016-04-18
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    相关资源
    最近更新 更多