Page 1 of 1

[Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 9:07 am
by leosa99
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: Select all

[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"
 
...

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 9:36 am
by MichaelFarthing
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'.

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 10:17 am
by leosa99
Thanks for the reply !
I replaced the push, now it pushes the value, but it still doesn't work.

Code: Select all

[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"

...

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 10:33 am
by sleephacker
leosa99 wrote:Thanks for the reply !
I replaced the push, now it pushes the value, but it still doesn't work.

Code: Select all

[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.

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 10:41 am
by leosa99
OK i see the mistake, THANKS !

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 11:10 am
by MichaelFarthing
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: Select all

        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: Select all

       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.

Re: [Nasm] Get pushed value.

Posted: Sat Dec 17, 2016 12:13 pm
by leosa99
@MichaelFarthing The problem is well explained by @sleephacker.