Sign in to follow this  
andrew1123

What's wrong with this NASM code...

Recommended Posts

...I'm new to NASM (and ASM for that matter) When I have STRING as "HELLO", it displays "HELLO". When I have STRING as "Hello", it displays "llo". BITS 16 ORG 07C00h MAIN: STRING DB "Hello",0 MOV SI,STRING CALL PR_STRING MOV AH,00h INT 16h INT 20h PR_STRING: LODSB OR AL,AL JZ RETURN MOV AH,0Eh INT 10h JMP PR_STRING RETURN: RET TIMES 510 - ($-$$) DB 0 DW 0AA55h Can anyone help me find the problem with the code? Thanks. [Edited by - andrew1123 on May 2, 2007 10:03:00 PM]

Share this post


Link to post
Share on other sites
i put MAIN before the rest, so I don't need to jump. But I'm gonna try putting the rest before MAIN and adding JMP MAIN. And yes, I'm trying out my first boot-loader.


Nope, when I add JMP, for some reason, it displays some weird 8 looking ascii symbol. I can't use call either. Maybe if I add STRING before JMP...

That doesn't work, either.

Share this post


Link to post
Share on other sites
(Im surprised your code didnt triple fault)

Either add "jmp MAIN" before your data, or move your data at the END
of the program ie:

BITS 16
ORG 07C00h

MAIN:
MOV SI,STRING
CALL PR_STRING
MOV AH,00h
INT 16h
INT 20h

PR_STRING:
LODSB
MOV AH,0Eh
OR AL,AL
JZ RETRN
INT 10h
JMP PR_STRING

RETRN:
RET

STRING DB "Hello",0

TIMES 510 - ($-$$) DB 0
DW 0AA55h

Everything else looks fine, besides your RET instruction (Where will it return to?)

Because the bootloader is pure binary, execution starts from the first line
of your file--whether its data or not. This is why you would need to "jump
over your data"


Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
(Im surprised your code didnt triple fault)

Either add "jmp MAIN" before your data, or move your data at the END
of the program ie:

BITS 16
ORG 07C00h

MAIN:
MOV SI,STRING
CALL PR_STRING
MOV AH,00h
INT 16h
INT 20h

PR_STRING:
LODSB
MOV AH,0Eh
OR AL,AL
JZ RETRN
INT 10h
JMP PR_STRING

RETRN:
RET

STRING DB "Hello",0

TIMES 510 - ($-$$) DB 0
DW 0AA55h

Everything else looks fine, besides your RET instruction (Where will it return to?)


It returns to after CALL PR_STRING

BTW, moving the DATA didn't do anything. It now displays nothing. And I tried using JMP MAIN, but that screwed it up even more.

Share this post


Link to post
Share on other sites
Also, you are still relying on DOS interrupts:


MOV SI,STRING
CALL PR_STRING
MOV AH,00h
INT 16h
INT 20h

AH=0 INT 20h is a DOS interrupt, not BIOS. Use a HLT instruction to terminate instead.

INT 16h returns the scancode in AH, so INT 21h will execute some random
interrupt. I dont think you want this[smile]

Also, CALL pushed the return address on the stack, but you havnt set up
a stack yet.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Also, you are still relying on DOS interrupts:


MOV SI,STRING
CALL PR_STRING
MOV AH,00h
INT 16h
INT 20h

AH=0 INT 20h is a DOS interrupt, not BIOS. Use a HLT instruction to terminate instead.

INT 16h returns the scancode in AH, so INT 21h will execute some random
interrupt. I dont think you want this[smile]


i changed that to HLT, forgot to write it here. anyway, whats my environment?

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Compilier/Linker/emulator etc..

Also, CALL pushes the return address on the stack, but you havnt set up
a stack yet. This could be the problem.


im just assembling it in NASM as bin.

How do I set up a stack? I thought it was already set up. Is that because that NASM doesn't use ASSUME?

Share this post


Link to post
Share on other sites
Corrected code:


BITS 16
ORG 07C00h

JMP MAIN

STRING DB "Hello",0

MAIN:

CLI
XOR AX, AX
MOV DS, AX
MOV AX, 09000h
MOV SS, AX
MOV SP, 0FFFFh

MOV SI,STRING
CALL PR_STRING
HLT

PR_STRING:
LODSB
OR AL,AL
JZ RETURN
MOV AH,0Eh
INT 10h
JMP PR_STRING

RETURN:
RET

TIMES 510 - ($-$$) DB 0
DW 0AA55h




Quote:

How do I set up a stack? I thought it was already set up. Is that because that NASM doesn't use ASSUME?

Absolutley *nothing* is setup for you. You have to set it up.

The above code sets the stack from 09000h-0FFFFh.

Hope this helps[smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Corrected code:
*** Source Snippet Removed ***
Quote:

How do I set up a stack? I thought it was already set up. Is that because that NASM doesn't use ASSUME?

Absolutley *nothing* is setup for you--you have to do *everything* in kernel land.

The above code sets the stack from 09000h-0FFFh.

Hope this helps[smile]


It doesn't print anything. Just exits right away (cuz you took out the INT 16h bit).

Share this post


Link to post
Share on other sites
It works--I tested it.

Im assuming your envirement is different (or a different emulator)

The HLT instruction in real mode litterally stops the processor,
hence it should stop there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
It works--I tested it.

Im assuming your envirement is different (or a different emulator)


im just running it straight thru windows by clicking the .com file.

Share this post


Link to post
Share on other sites
Quote:

im just running it straight thru windows by clicking the .com file.

Thats the problem[smile]

*.COM programs are emulated through a 32bit NTVDM. All BIOS calls will be
ignored. (They can be emulated though)

Your instructions have to be executed directly by the processor, not
through another OS. This is why you either need an emulator (I use Bochs)
or copy it to the bootsector of a floppy and boot from it. You can also
use a virtual floppy image on a CD, and boot from CD. (It will be treated
as a floppy)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this