首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序集8086中的堆栈错误(实模式)(堆栈已满时打印字符)

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

Stack Overflow用户
提问于 2013-03-12 04:52:31
回答 1查看 685关注 0票数 0

所以我写了一个小程序,把一个大于9的数字写到屏幕上。但是一旦我把一个值推入栈,我就不能打印任何东西,直到栈是空的。有没有办法绕过这个问题?

代码如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-12 05:30:03

你的代码中有两个bug。

第一个问题是在div cx之前不会将dx置零

代码语言:javascript
复制
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除以,这没有多大意义。

另一个在这里:

代码语言:javascript
复制
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)

代码语言:javascript
复制
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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15348376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档