Asm question

Programming, for all ages and all languages.
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Asm question

Post by tera4d »

Hello!

Currently I am learning the assembly language. (I do not own any books so I am learning from the internet)
And I had a question about the stack.
Here is my script:

Code: Select all

xor ax, ax
xor bx, ax
mov ax,8
push ax
mov bx,7
push bx
pop ax
First I set bx and ax to 0. Then I put a 8 into ax and push it onto the stack. Then I put a 7 into bx and put it onto the stack. Now the stack has a total of 15 from ax and bx.
Then I get ax out of the stack. If I am not mistaken the stack now only contains the 7 from bx right?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Asm question

Post by ru2aqare »

tera4d wrote:First I set bx and ax to 0. Then I put a 8 into ax and push it onto the stack. Then I put a 7 into bx and put it onto the stack. Now the stack has a total of 15 from ax and bx.
Sort of. The stack contains two items - the 8 from register ax and 7 from register bx. They are not added together.
tera4d wrote: Then I get ax out of the stack. If I am not mistaken the stack now only contains the 7 from bx right?
No. When you pop an item off the stack, you always get what you pushed most recently - the 7 from bx in your example.
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

@ru2aqare:
So the pop ax will retreive 7 because that is the last that is pushed to it ?
I think I get it =)
Thank you for your fast reply!

But all I have read about is programs being executed by a operating system so the os assigns a STACK to the program.
But how about declaring my own stack, How would I declare my own stack?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Asm question

Post by thepowersgang »

Well, first you allocate space for your new stack (using say resb 512 in your BSS section)

Next you set ss:sp to the end of your allocated space (mov ss, seg stack; mov sp, ofs stack)

(also, in the second line of your first post, you did not alter bx - you need to do xor bx,bx to zero it)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

@thepowersgang:
Ohh I see!
About the BSS section forgive me for this question but I am still learning.
How can I use it? As I am using just a empty notepad file and save it as a .asm and compile with : nasm -f bin test.asm -o test.o
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Asm question

Post by thepowersgang »

I suggest learning to look things up, but just because I'm feeling nice, here's how

Code: Select all


[section .text]
    ; Code goes here

[section .data]
    ; Data goes here
someValue:
    dd  0x0badbeef

[section .bss]
    resb 1024
stack:

Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

@thepowersgang:
Thank you ^^
Well about looking up I only found things involving linker scripts. But within the file I couldnt find something. I think that I am just looking at the wrong places haha.

Anyway thank you all!
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Asm question

Post by qw »

If you're learning assembly from the net, you'd best look here: http://homepage.mac.com/randyhyde/webst ... index.html.

Good luck!

Roel
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

@Hobbes:
Thank you very much for the helpfull link :D I have not seen this one before so I will definitely look at the provided courses!
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Asm question

Post by qw »

You're welcome! It's probably the best text on the subject on the net.

Roel
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

I studied asm some more. And I also came across this great program! http://www.emu8086.com
And I tried to make a ASM script and it worked (perfectly in emu8060) but in bochs and virtual pc I only see a blinking cursor.
Here is my script:

(It sets up video mode 13h and then prints a string)

Code: Select all


bits 16
org 7c00h
      
start: jmp prog

msg db "Hello, Joker!",0    

prog:        
    mov bp, 7c02h 
    mov es, si

    xor ax, ax
    mov ah, 0
    mov al, 13h
    int 10h

    xor ax, ax
    mov bx, ax
    mov cx, ax
	mov ah, 13h
	mov al, 0
	mov bh, 0
	mov bl, 100b
	mov cx, 13
	mov dh, 5
	mov dl, 5  
    int 10h

    cli
    hlt

TIMES 510 - ($-$$) db 0
DW 0xAA55
Can someone please help me finding the mistake? As I am unable to
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: Asm question

Post by Coty »

try this.

Code: Select all

bits 16
org 7c00h
     
start: jmp prog
msg db "Hello, Joker!",0   

prog:   
    mov     ah, 00h        ; set default video mode   
    mov     al, 03h         ; 80 collotions x 25 rows 
    int     10h                ; Do it. 

   lea si, msg        ; Move the message to print into SI
   Call  PrintString   ; Print text to the screen.
   jmp  $                 ; Halt PC

PrintString:
     mov   AH, 0Eh    ; Request print. 
p_loop: 
   lodsb                  ; Load byte from SI into AL. 
    cmp    AL, 0       ; is ALs value 0?   
    jz        p_done    ; If so were done here.    
    int      10h          ; use BIOS to Print charictar. 
    jmp     p_loop     ; Do it again. 
p_done: 
    ret              ; Return to call.  

TIMES 510 - ($-$$) db 0   ; Add pading to make loader 512 bytes
DW 0xAA55                    ; two extra bytes that make up a boot signature for the full 512.
note: not tested but it should work.

PS: emu8086 only emulates the old 8086 processor, Also do not right the image using emu8086
unless your going to run it on real hardware. [-X other emulators don't seem to like the image
some reason :(
Last edited by Coty on Fri Jan 29, 2010 4:45 pm, edited 1 time in total.
My hero, is Mel.
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

@Coddy:
Thanks man. But I already wrote a routine like that one and it also worked flawlessly. But I wanted to go on and do something with a VGA mode (13h is the most simple one i've read) but it didnt work strangely. But as far as I know the complete x86 cpu line is backwards compatible right? http://www.ctyme.com/intr/int-10.htm < I have used this page as a reference.
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: Asm question

Post by Coty »

> But as far as I know the complete x86 cpu line is backwards compatible right?
Yes, but what I am saying is for some res eon emu8086 does not create an FDD image that works with most emulators.

Oh, and that you'll be limited with your coding with it. But it is a fine choice for beginners, as long as you do not mind it being slow.
My hero, is Mel.
tera4d
Member
Member
Posts: 37
Joined: Sun Apr 26, 2009 11:22 am

Re: Asm question

Post by tera4d »

I assembled the code with : nasm -f bin boot.asm -o boot.bin
And then I added it to a floppy image with : winimage
Virtual pc / bochs both boot from it but then all I see is a blinking cursor.
I use emu8086 just to learn ASM (and as a great code editor). < also emu8086 uses FASM =D
But with the code I pasted above, well it has nothing to do with emu8086 it is just tested on it but it was written in notepad++ and assembled with NASM
Post Reply