Author Topic: Newbie Questions: db directive?  (Read 3966 times)

Offline artificer

  • Jr. Member
  • *
  • Posts: 5
Newbie Questions: db directive?
« on: February 13, 2013, 09:27:17 PM »
Please check this code fragment taken from: http://mikeos.berlios.de/write-your-own-os.html

mov si, text_string   ; Put string position into SI
call print_string      ; Call our string-printing routine
jmp $                 ; Jump here - infinite loop!

text_string db 'This is my cool new OS!', 0

Question 1: the first line is copying text_string location into the si register. Can it be done when text_string is declared several lines later?

Question 2:  compare this two lines also taken from the same url an tell me if they do the same.

1)mylabel: db 'Message here', 0
2)mylabel db 'Message here', 0

Question 3: Where can I find "detailed" documentation for the db directive in NASM?
« Last Edit: February 13, 2013, 09:33:23 PM by artificer »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Newbie Questions: db directive?
« Reply #1 on: February 14, 2013, 12:03:12 AM »
The answer to question 3 contains the answer to the first two as well: http://www.nasm.us/docs.php

Offline artificer

  • Jr. Member
  • *
  • Posts: 5
Re: Newbie Questions: db directive?
« Reply #2 on: February 14, 2013, 04:06:38 AM »
Thanks for your answer however I dit browse that page before posting and I think there is no definitive answer for question 1 in there. However please feel free to educate me if I am wrong!

PD: I did not read the whole documentantion of course I was focus on this one: http://www.nasm.us/xdoc/2.10.07/html/nasmdoc3.html#section-3.2.1
« Last Edit: February 14, 2013, 04:08:56 AM by artificer »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2359
  • Country: us
Re: Newbie Questions: db directive?
« Reply #3 on: February 14, 2013, 05:52:27 AM »
1) Sure. Try it:
Code: [Select]
BITS 16

start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096

mov ax, 07C0h ; Set data segment to where we're loaded
mov ds, ax


mov si, text_string ; Put string position into SI
call print_string ; Call our string-printing routine

jmp $ ; Jump here - infinite loop!

print_string: ; Routine: output string in SI to screen
mov ah, 0Eh ; int 10h 'print char' function

.repeat:
lodsb ; Get character from string
cmp al, 0
je .done ; If char is zero, end of string
int 10h ; Otherwise, print it
jmp .repeat

.done:
ret

text_string db 'This is my cool new OS!', 0


times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature

Or before...

Code: [Select]
BITS 16

        jmp start
        nop

text_string db 'This is my cool new OS!', 0

start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096

mov ax, 07C0h ; Set data segment to where we're loaded
mov ds, ax


mov si, text_string ; Put string position into SI
call print_string ; Call our string-printing routine

jmp $ ; Jump here - infinite loop!

print_string: ; Routine: output string in SI to screen
mov ah, 0Eh ; int 10h 'print char' function

.repeat:
lodsb ; Get character from string
cmp al, 0
je .done ; If char is zero, end of string
int 10h ; Otherwise, print it
jmp .repeat

.done:
ret



times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature

In fact, a bootsector is "supposed" to start with a short jmp and a nop, or a near jmp, followed by some disk information. You might even encounter a BIOS that cares. I've never seen it, but I've heard from a guy who was getting "no system disk" from his BIOS. I suggested he try starting with a jmp, and he said it worked. Very rare!

You wouldn't want to put data someplace where it's going to get executed... or after the end of your bootsector.

2) Yes, they're the same. You never "need" a colon after a label. Nasm will warn if there's a label alone on a line without a colon, but it works. This warning can be turned off... and used to be off by default. It was turned on by default because it serves as a sort of spellchecker:

Code: [Select]
.repeat:
lodbs ; Get character from string

Nasm doesn't know what "lodbs" is, and would silently assemble it as a label-without-a-colon without this warning. Otherwise, either with or without a colon is fine... (you can try this too)

3) Yeah, you've found the Friendly Manual. Sometimes doesn't tell you exactly what you want to know, sometimes tells you too much. Best we can do, at the moment.

You don't ask, but a bootsector is not the easiest thing to write as a first project!

Best,
Frank


Offline artificer

  • Jr. Member
  • *
  • Posts: 5
Re: Newbie Questions: db directive?
« Reply #4 on: February 14, 2013, 06:15:54 AM »
Thanks a lot for your friendly response Frank!

I take that the NASM compiler will process the db directives before actually compiling the ASM instructions so they work no matter where I put them right?

I am a software developer but not an ASM developer. I am studying the mikeOS as a hobby so I only intend to learn enough ASM to understand the mikeOS source code and I have no hurry.

Is OK to ask basic NASM questions in this forum?
« Last Edit: February 14, 2013, 06:18:22 AM by artificer »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2359
  • Country: us
Re: Newbie Questions: db directive?
« Reply #5 on: February 14, 2013, 08:45:38 AM »
Nasm makes multiple passes over your source code, so it should find everything. Strictly speaking, it finds the label, not the "db" itself. "db" (and friends) are referred to as "pseudo-instructions" not "directives", but the distinction is trivial.

You can ask any question you want. You might get "RTFM" as an answer, but if we're feeling friendly we might tell you where in the manual... :)

Best,
Frank


Offline artificer

  • Jr. Member
  • *
  • Posts: 5
Re: Newbie Questions: db directive?
« Reply #6 on: February 14, 2013, 01:34:17 PM »
Thanks! I appreciate your answers! It actually surprise me that I have to make jumps in order to avoid the label (db) directives to be executed. I was expecting the other way around cause in my head I compared it to a declare variable statement. Also I did not find "so far" any warning in the FM about avoid executing it.
« Last Edit: February 14, 2013, 01:39:07 PM by artificer »

Offline artificer

  • Jr. Member
  • *
  • Posts: 5
Re: Newbie Questions: db directive?
« Reply #7 on: February 14, 2013, 06:01:38 PM »
OK I have a theory about how it Works please tell if I am wright or wrong!

1)The whole programs is compiled into machine code including the db label directives. During the compilation any use of the db labels are replaced with its actual memory location or value as needed.
2)Before execution the whole machine code including the memory information defined by db labels is stored in RAM.
3)During the program execution there is no need for executing the db labels since the information they represent is already stored on RAM.

If I am right them it means that higher level languages like C and C# actually encapsulate a lot more than I originally imagined.

Offline Gerhard

  • Jr. Member
  • *
  • Posts: 50
Re: Newbie Questions: db directive?
« Reply #8 on: February 15, 2013, 04:10:25 PM »
Hi artificer,

If I am right them it means that higher level languages like C and C# actually encapsulate a lot more than I originally imagined.

sure. There's a lot of overhead produced by compilers.

Gerhard

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Newbie Questions: db directive?
« Reply #9 on: February 16, 2013, 12:34:11 AM »
There's a lot of overhead produced by compilers.

Genericism costs no matter what the subject matter is, but optimized solutions always cost more.

Time, price, quality... pick any two ;)