News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Newbie Questions: db directive?

Started by artificer, February 14, 2013, 03:14:57 PM

Previous topic - Next topic

artificer

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?

PD: I now realize that I should have posted this question on the the campus but I can not move it now! So I apologize!

jj2007

Quote from: artificer on February 14, 2013, 03:14:57 PMQuestion 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?

No idea. But it looks like an interesting question. Can you please declare it several lines later and try to assemble it, and then tell us if it worked? Thank you.

japheth


Your code most likely is supposed to be assembled with Nasm. You might be in the wrong forum.

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

For Nasm there's no difference. Recent versions of Masm won't accept line 1, unless you use cmdline option -Zm. But even if masm will accept both lines, there's a difference: a ':' behind the label always defines a CODE label; the second line defines a DATA label. The difference between code and data labels is that the latter have a type.

MichaelW

#3
I did this as 16-bit code only because you posted 16-bit code.

;==============================================================================
.model small, C
.386
.stack
.data

    ;--------------------------------------------------------------
    ; test.asm(12) : error A2108: use of register assumed to ERROR
    ; test.asm(12) : error A2008: syntax error : db
    ;--------------------------------------------------------------

    ;text_string: db "This is my cool new OS!",13,10,0

    text_string db "This is my cool new OS!",13,10,0

.code
;==============================================================================
;------------------------------------------------------------------
; Writes a null-terminated string to display page 0 at the current
; cursor position, using the BIOS Teletype Output function.
;
; Interprets bell (07h), backspace (08h), CR (0Dh), and LF (0Ah)
; as control characters.
;------------------------------------------------------------------
print_string proc uses ax bx dx si npSrc:WORD
    mov si, npSrc
  @@:
    lodsb
    or  al, al
    jz  @F
    mov ah, 0Eh
    xor bh, bh
    int 10h
    jmp @B
  @@:
    ret
print_string  endp
;==============================================================================
.startup
;==============================================================================

    ;-----------------------------------------------------------------
    ; For the MOV instruction, like most 2-operand instructions, both
    ; operands must have the same size. The following is reported as
    ; an error because the DB directive allocates bytes and SI is a
    ; word (16-bit) register.
    ;-----------------------------------------------------------------

    ;mov si, text_string        ; error A2070: invalid instruction operands

    ;-----------------------------------------------------------------
    ; The following is OK because the operand sizes match, but all it
    ; will do is load the first byte of text_string ("T") into AL.
    ;-----------------------------------------------------------------

    mov al, text_string         ; OK

    ;-----------------------------------------------------------------
    ; Strings are usually accessed through pointers (IOW by address),
    ; so a common operation is to load the address of the string into
    ; a register, and since this example is 16-bit code the minimum
    ; register size is 16 bits.
    ;-----------------------------------------------------------------

    lea si, text_string         ; OK

    invoke print_string, si

    mov si, OFFSET text_string  ; OK

    invoke print_string, si

    mov si, OFFSET text_string1 ; OK

    mov si, OFFSET text_string2 ; OK

    invoke print_string, si

    xor ah, ah
    int 16h

;==============================================================================
.exit

; This is in the code segment:

    ;-----------------------------------------------
    ; test.asm(91) : error A2008: syntax error : db
    ;-----------------------------------------------

    ;text_string1: db "This is my cool new OS! 1",13,10,0

    text_string1 db "This is my cool new OS! 1",13,10,0

;------------------------------------------------------
; End the code segment and switch to the data segment.
;------------------------------------------------------

.data

    text_string2 db "This is my cool new OS! 2",13,10,0

end


For detailed information on the MASM directive see the Programmer's Guide. Sorry, I don't have a link handy.

BTW, I did not attempt to display text_string1 because with the small memory model a far pointer (were far means this it specifies a segment address and an offset address) would be required, and my display procedure is set up to handle only near pointers.
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

#4
Japheth is right - this code is written to be assembled with Nasm - and under linux
(refer to the Requirements section at the link you posted)
so - before you get meaningful answers, it is best if we know which assembler and OS you are using to build

hopefully, the OS isn't too critical   :P
but, the assembler choice certainly is
a generic answer would be to get the manual for the assembler you are using

artificer

Thanks! I was expecting that given the similarities the same code will apply to MASM. I will read forward!

Gunther

Hi artificer,

Quote from: artificer on February 14, 2013, 03:14:57 PM
Question 3: Where can I find "detailed" documentation for the db directive?

you should find the information here: http://www.nasm.us/doc/nasmdoc0.html

Gunther
You have to know the facts before you can distort them.