Page 1 of 1
How to display more than one string in NASM
Posted: Sat Mar 27, 2010 2:29 am
by mooseman
Hi all -
I'm a first-timer here, and looking forward to some interesting discussions!
I've just been poking around with the "Hello world" bootloader from the "Bona Fide OS development" site, and am trying to display more than one string. ( I managed to assemble and run the one-string version with no problems).
However, when I try to run the code below, I get the error -
"hello_world_bootloader.txt:44: error: comma or end of line expected
Puzzling, as the string has the usual 13,10,0 at the end of it. Anyway, here is the code -
Code: Select all
[BITS 16] ; 16 bit code generation
[ORG 0x7C00] ; Origin location
; Main program
main: ; Label for the start of the main program
mov ax,0x0000 ; Setup the Data Segment register
; Location of data is DS:Offset
mov ds,ax ; This can not be loaded directly it has to be in two steps.
; 'mov ds, 0x0000' will NOT work due to limitations on the CPU
mov si, HelloWorld ; Load the string into position for the procedure.
call PutStr ; Call/start the procedure
mov si, Test ; Load another string
call PutStr ; Print string
jmp $ ; Never ending loop
; Procedures
PutStr: ; Procedure label/start
; Set up the registers for the interrupt call
mov ah,0x0E ; The function to display a chacter (teletype)
mov bh,0x00 ; Page number
mov bl,0x07 ; Normal text attribute
.nextchar ; Internal label (needed to loop round for the next character)
lodsb ; I think of this as LOaD String Block
; (Not sure if thats the real meaning though)
; Loads [SI] into AL and increases SI by one
; Check for end of string '0'
or al,al ; Sets the zero flag if al = 0
; (OR outputs 0's where there is a zero bit in the register)
jz .return ; If the zero flag has been set go to the end of the procedure.
; Zero flag gets set when an instruction returns 0 as the answer.
int 0x10 ; Run the BIOS video interrupt
jmp .nextchar ; Loop back round tothe top
.return ; Label at the end to jump to when complete
ret ; Return to main program
; Data
HelloWorld db 'Open the pod-bay door, HAL...',13,10,0
Test db 'Another nice long string',13,10,0 ; the error is happening here
; End Matter
times 510-($-$$) db 0 ; Fill the rest with zeros
dw 0xAA55 ; Boot loader signature
So, any help in fixing this so that I can display more than one text string would be great - many thanks in advance....
- mooseman
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 2:40 am
by qw
Hi Mooseman, welcome to the forum.
"Test" on the x86 is an instruction. If you insist on using it as a label, you must prefix it with a $, but I advise you to choose another name.
Roel
P.S. This is the first boot sector I've ever seen that opens the pod bay door.
P.P.S. Some additional advise: set up a stack, clear the DF flag and enable interrupts explicitly. This will save you trouble later.
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 2:45 am
by xenos
"test" is the name of an x86 instruction, so NASM interprets the faulting line as "test [some registers]", and then fails because "db 'Another nice long string',13,10,0" is not a valid register combination. Try using a different label and it should work.
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 2:46 am
by qw
XenOS, is it really Friday, August 12, 2005 in Germany? It's Monday, January 26, 2009 in the Netherlands.
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 3:10 am
by mooseman
Hi again all -
Thanks very much for that, Hobbes and XenOS! I changed the label and it does indeed now work!
( Sheesh, I would have never guessed that "test" was actually a "built-in" instruction....
)
As you will have guessed, I'm just getting to assembly coding. I think that one of the first areas that I want to become completely familiar with is the registers. By that, I mean what each of them can be used for, and any restrictions that they may have. After that, I'll then get into becoming more familiar with the various mnemonics.
Oh, and yeah... I thought I'd put in the quote from "2001".... might as well have some fun while I'm doing this....
Oh, and thanks for the other advice too, Hobbes! It may be a little while before I fully understand what you mean though.... I know what a "stack" is, but that's about it....
So - how would I do the things that you've mentioned?
Thanks again, bye for now -
- mooseman
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 1:03 pm
by Gigasoft
@Hobbes: It's Saturday March 27, 2010 in both Germany and the Netherlands, as well as all other European countries. We all use the same calendar. As an OS developer, one would think you knew which year it is
You registered at the forum on January 26, 2009 and he registered on August 12, 2005.
@mooseman: Just set ss and sp to point to the end of the area you want to use for the stack. CLD clears the direction flag. For example:
The stack will then begin at 10000h (since the first word pushed will be stored at 0:0fffeh).
Re: How to display more than one string in NASM
Posted: Sat Mar 27, 2010 3:25 pm
by mooseman
Gigasoft wrote: (snip)
@mooseman: Just set ss and sp to point to the end of the area you want to use for the stack. CLD clears the direction flag. For example:
The stack will then begin at 10000h (since the first word pushed will be stored at 0:0fffeh).
Ahh... thanks very much for that, Gigasoft!
Yes... as a beginner, I'll have to "read up" a bit on ss, sp and cld. They're things that I'm not familiar with (but I'm sure that I'll eventually get to know them well....
)
Thanks again - bye for now -
- mooseman
Re: How to display more than one string in NASM
Posted: Sun Mar 28, 2010 4:14 am
by qw
Gigasoft wrote:@Hobbes: It's Saturday March 27, 2010 in both Germany and the Netherlands, as well as all other European countries. We all use the same calendar. As an OS developer, one would think you knew which year it is
You registered at the forum on January 26, 2009 and he registered on August 12, 2005.
Do I feel stupid...
Gigasoft wrote:@mooseman: Just set ss and sp to point to the end of the area you want to use for the stack. CLD clears the direction flag. For example:
The stack will then begin at 10000h (since the first word pushed will be stored at 0:0fffeh).
Warning: if an interrupt happens between the first two lines, you will probably crash. Disable interrupts first (CLI) and enable them afterwards (STI).
Roel
Re: How to display more than one string in NASM
Posted: Sun Mar 28, 2010 4:18 am
by osdnlo
Hobbes wrote:XenOS, is it really Friday, August 12, 2005 in Germany? It's Monday, January 26, 2009 in the Netherlands.
Gigasoft wrote:@Hobbes: It's Saturday March 27, 2010 in both Germany and the Netherlands, as well as all other European countries. We all use the same calendar. As an OS developer, one would think you knew which year it is
You registered at the forum on January 26, 2009 and he registered on August 12, 2005.
Hobbes wrote:Do I feel stupid...
Classic.
Re: How to display more than one string in NASM
Posted: Mon Mar 29, 2010 12:13 am
by mooseman
Gigasoft wrote:@mooseman: Just set ss and sp to point to the end of the area you want to use for the stack. CLD clears the direction flag. For example:
The stack will then begin at 10000h (since the first word pushed will be stored at 0:0fffeh).
Warning: if an interrupt happens between the first two lines, you will probably crash. Disable interrupts first (CLI) and enable them afterwards (STI).
Roel
Ok - thanks for that additional info too (as I didn't know how to disable and reenable interrupts).
Just a few questions -
a) For bootloaders, is it mandatory that the "origin location" be 0x7C00? I assume it probably is - just wanting to make sure. ( It'd make sense to have a fixed location for them).
b) You've mentioned above that "the first word pushed to the stack will be stored at 0:0fffeh" Is that
always the case that the first word is stored at that address? What I'm getting at is - how do I tell where a stack begins and ends? The code "xor sp sp" doesn't seem to give an address - that's what is puzzling me. I'm not sure how you know that the first word is stored at 0:0fffeh.
Bye for now -
- mooseman
Re: How to display more than one string in NASM
Posted: Mon Mar 29, 2010 12:33 am
by Solar
mooseman wrote:a) For bootloaders, is it mandatory that the "origin location" be 0x7C00?
That's where the BIOS will load the MBR, correct.
What I'm getting at is - how do I tell where a stack begins and ends?
Check out "segment registers" / "segment selectors".
The code "xor sp sp" doesn't seem to give an address - that's what is puzzling me.
It sets the "sp" segment register to zero. See above.
PS: I've never even written a "hello world" in assembler, and here I am giving advice as if I knew it all...
Re: How to display more than one string in NASM
Posted: Mon Mar 29, 2010 1:23 am
by qw
mooseman wrote:a) For bootloaders, is it mandatory that the "origin location" be 0x7C00? I assume it probably is - just wanting to make sure. (It'd make sense to have a fixed location for them).
Yes, boot sectors are always loaded at physical address 07C00H. So if you're accessing your data relative to segment address 0000H, the origin must be 7C00H. Here is a thread about that topic:
[ORG 0x7C00] vs mov ax, 0x07C0 vs jmp 0x07C0:start. You should understand the x86 segmented addressing scheme.
mooseman wrote:b) You've mentioned above that "the first word pushed to the stack will be stored at 0:0fffeh" Is that always the case that the first word is stored at that address? What I'm getting at is - how do I tell where a stack begins and ends? The code "xor sp sp" doesn't seem to give an address - that's what is puzzling me. I'm not sure how you know that the first word is stored at 0:0fffeh.
XOR SP, SP does exactly what it sais: it XORs SP with itself. The result is always zero, so SS:SP is initialized to 0000:0000H or physical address 00000H. Now if a value is PUSHed on the stack, SP is decremented with two and then the value is stored. So after the first PUSH, SS:SP is 0000:FFFEH which means that the top of stack is effectively 10000H.
Roel
P.S. The things you are asking are quite elementary and you'd better learn them before diving into OS development. Google for "Art of Assembly Language", it's a great online tutorial.
Re: How to display more than one string in NASM
Posted: Mon Mar 29, 2010 1:46 am
by mooseman
Hi again -
Thanks very much for those replies, Solar and Hobbes - they explain things nicely.
( I'll check out "Art of Assembly Language" too - thanks for that. )
Agreed, my questions are very elementary, and I also agree with learning more before "jumping in too deep" into OS development. Anyway, I'm not in much of a hurry...
I'm *very much* enjoying myself anyway, finding out more about assembly language!
- mooseman
Re: How to display more than one string in NASM
Posted: Mon Mar 29, 2010 2:08 am
by qw
mooseman wrote:I'm *very much* enjoying myself anyway, finding out more about assembly language!
Well, that's what it's all about, isn't it? Keep enjoying!
Roel