Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does popa and pusha actually works?

I am attempting to write a simple OS from scrath, but I have stumbled into a problem.
I wrote a simple procedure that runs through a string and prints it on the screen.

print_string:
    pusha
    cmp byte[bx], 0    
    je exit             
    mov ah, 0x0e        
    mov al, [bx]        
    int 0x10            
    inc bx              
    jmp print_string    
    
    exit: 
        mov ah, 0x0e       
        mov al, 13          
        int 0x10            
        mov al, 10          
        int 0x10            
        popa
        ret                 

And I am including it on the main file.

[org 0x7c00]              

mov bx, hello               
call print_string           
mov bx, hi                 
call print_string          
jmp $                       

%include "print_string.s"   
hello:                  
    db "Hello, World!",0     
hi:
    db "This is a test.",0

times 510-($-$$) db 0       
dw 0xaa55                   

But for some reason, instead of printing Hello, World! This is a test. it just prints Hello World!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I remove the pusha and popa from print_string.s and place it on the main file like this:

[org 0x7c00]              

mov bx, hello    
 
pusha          
call print_string 
popa      
    
mov bx, hi   

pusha               
call print_string   
popa 
       
jmp $                       

%include "print_string.s"   
hello:                  
    db "Hello, World!",0     
hi:
    db "This is a test.",0

times 510-($-$$) db 0       
dw 0xaa55                   

It works just fine. Why?

>Solution :

print_string is called in the loop, on each iteration is does pusha. But there is only one popa instruction for multiple pusha. A fix:

print_string:
    pusha
next_char:
    mov al, [bx]        
    or al, al
    je exit             
    mov ah, 0x0e        
    int 0x10            
    inc bx              
    jmp next_char   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading