OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Nov 22, 2017 2:20 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 9:07 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 15
Hi !
It problably seems like a dumb question but I don't find how to get a pushed value inside a function.
For example :
Code:
[org 0x7C00]
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov ax, 0x1000
mov sp, ax ; Stack --> 0x0:0x1000
 
push letter
call print ; Add return address on the stack.
jmp $
 
print:
mov ah, 0x0E
mov si, sp
add si, 4d ; Go to the start of the stack. One value pushed on the stack is 16 bit size.
mov al, [si] ; Should get the X
int 0x10
ret
 
letter: db "X"
 
...


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 9:36 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 10, 2016 7:35 am
Posts: 130
Location: Lancaster, England, Disunited Kingdom
I think you should be doing:
push [letter]
i.e. push the value at address 'letter' rather than push the address 'letter'

The print function looks ok to me, though you could do:
mov al [si+4]
instead of
add si 4
mov al [si]

(but what you have done is perfectly OK too)

It's also more usual to use bp rather si for accessing from the stack because bp uses ss by default whereas si uses ds.
Obviously, here, you have set ds=ss so there is no problem - it's just a case of a bit of 'defensive programming'.


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 10:17 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 15
Thanks for the reply !
I replaced the push, now it pushes the value, but it still doesn't work.
Code:
[org 0x7C00]
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov ax, 0x1000
mov sp, ax ; Stack --> 0x0:0x1000

mov ax, [letter]
push ax

call print ; Add return address on the stack.
jmp $

print:
mov ah, 0x0E
mov si, sp
add si, 4d ; Go to the start of the stack. One value pushed on the stack is 16 bit size.
mov al, [si] ; Should get the X
int 0x10
ret

letter: db "X"

...


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 10:33 am 
Offline
Member
Member
User avatar

Joined: Thu Aug 06, 2015 6:41 am
Posts: 85
Location: Netherlands
leosa99 wrote:
Thanks for the reply !
I replaced the push, now it pushes the value, but it still doesn't work.
Code:
[org 0x7C00]
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov ax, 0x1000
mov sp, ax ; Stack --> 0x0:0x1000

mov ax, [letter]
push ax

call print ; Add return address on the stack.
jmp $

print:
mov ah, 0x0E
mov si, sp
add si, 4d ; Go to the start of the stack. One value pushed on the stack is 16 bit size.
mov al, [si] ; Should get the X
int 0x10
ret

letter: db "X"

...

You should add 2 instead of 4 to si, because sp always points to the value last pushed on the stack, which in this case is a 16bit (2 bytes) return address, and your input character comes right after that.
You might also want to make sure the bh register is set to 0 (or some other value, as long as you don't leave it as it is) because in text modes int 10h, ah = 0Eh also takes bh as input.

_________________
Lithik


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 10:41 am 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 15
OK i see the mistake, THANKS !


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 11:10 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 10, 2016 7:35 am
Posts: 130
Location: Lancaster, England, Disunited Kingdom
OK, bit more info would be helpful.

[Ah while writing I see you've sorted it. Tell us the problem (a) out of interest and (b) because one day it might help someone else]

Some of what I was writing might still be useful to you so I'm posting it anyway:


I would also suggest that if you are using a non-graphics mode you stop using this interrupt entirely and simply write to the actual video memory. For the usual 80x25 mode this is at 0xB8000 so in real mode requires use of a segment register not set to 0. In my bootloader I set fs to point to the start of video memory like this

Code:
        mov    bx   0xb800   ;bb 00 b8  [The value in fs must be mentally shifted one hex place left to get the value it actually point to]
   mov    fs   bx      ;8e e3


I can then write the letter A directly to the top left of screen like this:

Code:
       mov ax 0x0741   
       xor di, di          ;quickly sets di to zero to select the top left of screen
       mov [fs:di], ax

This needs a bit of explaining.
The video memory uses two bytes for each character cell. The first byte is the character to print - 0x41 or 'A', loaded to al in this example
The second byte is set to 0x07 and indicates the background colour to use and the colour for the foreground (ie the letter A itself) and is loaded to ah. Other colour possibilities are in the Wikki or readily found by a google.
In all 2000 bytes are used for the 25 rows of 80 columns at 2 bytes each.


Top
 Profile  
 
 Post subject: Re: [Nasm] Get pushed value.
PostPosted: Sat Dec 17, 2016 12:13 pm 
Offline

Joined: Tue Aug 30, 2016 9:34 am
Posts: 15
@MichaelFarthing The problem is well explained by @sleephacker.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group