Error with stack in assembly 8086 (real mode) (printing char while stack full

Question!

so I have written a little program to write a number higher than 9 to the screen. But as soon as I push a value to stack, I can't print a thing until the stack is empty. Is there a way to go around this?

Here is the code:

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


Answers

There are 2 bugs in your code.

The first one is is that you don't zero dx before 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

The problem is that you don't zero dx before division. The first time div cx, dx is uninitialized. The next time div cx is reached remainder:quotient gets divided by 10, and that does not make much sense.

The other one is here:

print_char:         ; Function to print single character to      screen
    mov ah, 0eh     ; Prepare registers to print the character
    int 10h         ; Call BIOS interrupt
    ret

You don't set any meaningful values to bl and bh, even if those are input registers of mov ah,0eh, int 10h (see 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)

As you use bx as a counter, you need to store it either inside or outside print_char. For example, saving bx inside 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 
By : nrz


This video can help you solving your question :)
By: admin