2013-03-11 63 views
0

所以我寫了一個小程序在屏幕上寫入一個高於9的數字。但是,只要我將值推入堆棧,我無法打印一個東西,直到堆棧爲空。有沒有辦法解決這個問題?程序集8086(實模式)中的堆棧出錯(堆棧滿時打印字符)

下面是代碼:

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 

回答

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,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 
    ret 
+0

謝謝。今天晚些時候會嘗試。我確實注意到了dx,一旦我嘗試反向打印數值(所以9573變成了3759,因爲這也會完成我所需要的print_int功能),但是我不知道bx的東西。所以謝謝你。 – ArdentAngel 2013-03-12 06:22:49

+0

是的,這工作完美。謝謝。 – ArdentAngel 2013-03-12 09:55:46