placing string into DS:SI

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Mr_Spam

placing string into DS:SI

Post by Mr_Spam »

i'm trying to print a string in my boot loader, but its not working. I belive its that my string is not being loaded correctly into DS:SI. The function looks something like this:

lds si, [msg] ;mov si, [msg] dosn't work either
print:
lodsb
cmp al, 0
je done
mov ah, 0Eh
int 10h
jmp print
done:

blah blah blah debugging crap then

msg dw "MESSAGE", 0

the int instruction in the loop is never executed, and it jumps to done in the first time through the loop.
thanks,

Mr_Spam
beyondsociety

Re:placing string into DS:SI

Post by beyondsociety »

Try this

Code: Select all

PrintMsg:

     mov ah, 0x0E          ; Teletype mode
     mov bh, 0x00          ; Page number

.nextchar:

     lodsb               ; Load [si] into al and increment si
     or al, al            ; Set the Zero Flag if al = 0
     jz .return         ; If the Zero Flag isset,  jumpto .return
     int 0x10           ; Call BIOS video function
     jmp .nextchar    ; Loop around to write next character

.return:

     ret                   ; Return
To call it, all you have to do is this:

Code: Select all

mov si, bootmsg
call PrintMsg

Code: Select all

bootmsg db 'Its working", 13, 10, 0
Hope this helps!
Curufir

Re:placing string into DS:SI

Post by Curufir »

Previous post's code looks good, use that.

Just wanted to clear up what seems to be a fairly common gotcha in NASM syntax.

Here goes nothing.

Code: Select all

vChar    db   0x8h
This code sets vChar to be the memory offset to the data. Ie the label vChar is a pointer to the actual memory location of the data, not the value of the data.

Code: Select all

mov ax, vChar
This moves the value of the pointer into ax. I think a C example might be something like someVar=*vChar (My C isn't too good so that may be an incorrect example, but the meaning should be plain)

Code: Select all

mov al, [vChar]
This moves the value of the byte (Because al is byte sized so the value is assumed byte sized) pointed to by vChar into register al. Once again, I think a C example would be simply someVar=vChar.

Don't mean to go on about it if this is something you already know, but it is essential in both C and ASM that people are comfortable with both the differences between pointers and values and how to handle them within their code.
.bdjames

Re:placing string into DS:SI

Post by .bdjames »

16 bit

lds si, [string]

32 bit

lea esi, [string]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:placing string into DS:SI

Post by Pype.Clicker »

.bdjames wrote: 16 bit

lds si, [string]

32 bit

lea esi, [string]
this assumes that strings is a segment:offset pointer, not an array of char to be displayed.
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

Its still not working :-\
heres what i have now:

mov si, msg
call displ
mov ax, 0E07h ;beep function
int 10 ;beep!
freez:
jmp freez
displ:
mov ah, 0Eh
mov bh, 0x0
.putch:
lodsb
or al, al
jz .done
int 10h
jmp .putch

.done:
ret
msg db "hello", 0

It beeps when its ran but it still dosn't display the string
for some reason i am most sceptical about the mov si, msg and the lodsb directives
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

after some more debugging of that function, i have found that the lodsb command loads 0 into al on the first attempt and all attempts after. what series of commands would replace the functionality of the lodsb command?
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

after even more and more hours of debugging, it has end up that even this simple code dosn't display crap, only moves the curser 1 char.

mov al, [msg]
mov ah, 0Eh
int 10h
freez: jmp freez

then...

msg db 61h ; letter a

???? what the h*ll? ??? ??? ???

this works:

mov al, 61h
mov ah, 0Eh
int 10h
freez: jmp freez

I am extreemly puzzeld. Through ours of debugging, al is always 0 because the contents of si is 0 after i mov si, msg in the original functions.

I am extreemly confused ??? ??? ??? ??? ???

any insight would be MUCH apreciated!!!

thanks,

Mr_Spam
Curufir

Re:placing string into DS:SI

Post by Curufir »

Ok, I'll admit that I just do not see how this is difficult. Anyhow, here's some code.

Code: Select all

;**********************************************************************
;* Simple print function for 0 terminated strings using teletype mode *
;* Takes DS:SI as a pointer to the string being printed               *
;**********************************************************************
BIOS_Print:
???push ax
???push bx
???mov ah, 0x0e
???mov bx, 0x0007
.1:
???lodsb
???or al, al
???jz short .2
???int 0x10
???jmp short .1
.2:
???pop bx
???pop ax
???retn
Here's how you would declare a string.

Code: Select all

vStr???db 'Hello World',13,10,0
Here's how to use it.

Code: Select all

???mov si, vStr
???call near BIOS_Print
Note that DS had better be a relevant segment, the value you should set it to depends on wether or not you are using org. This is a common gotcha, expecially after the pmode switch.

Amazingly enough this should look almost exactly like the previous example by beyondsociety. There's only a few ways to print strings in BIOS efficiently.
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

[attachment deleted by admin]
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

[attachment deleted by admin]
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

new break through after more debugging:

if i put a string into the varriable after the program starts executing, it prints the string.

mov dword [msg], 'test'
mov si, msg
call displ
jmp $
displ:
mov ah, 0Eh
mov, bh, 0
.putch
lodsb
cmp al, 0
je .done
int 10h
jmp .putch
.done:
ret
msg db 0
if i predeclare the varriable with the string it don't work.
any one know why or how to fix it?

thanks,

BK

msg
Iteration

Re:placing string into DS:SI

Post by Iteration »

BootSector Segment
Assume Cs:BootSector
Org 7C00h
Start:
mov ax,cs
mov ds,ax
mov bx,offset Boot_Message
mov cx,offset Boot_Message_delim - Boot_Message

Display_String:
mov al,byte ptr [bx]
inc bx
mov ah,0eh
push bx
mov bx,0007h
int 10h
pop bx
Loop Display_String

String_Done:
jmp String_Done







Boot_Message:
db "Hello World!",0
Boot_Message_delim:

BootSector ENDS
END Start
richie

Re:placing string into DS:SI

Post by richie »

I think the previous poster is right. As it seems to me you have a problem in reading data from variables and not printing data. So I think you make a mistake in setting ds right. The best ting you can do is using org 0x7c00 and than copying cs to ds:

Code: Select all

org 0x7c00

;Start of bootsector

Start:
 mov ax, cs
 mov ds, ax

; Now you should be able to access data
; ...

End:

;here you can put your data
Be careful that your bootsector is smaller than 512 bytes. If a variable has the value 0 but you set it to a non-zero value it would be possible that you did something wrong in addressing this variable. (at system bootup the whole memory is set to zero).

I hope that helps you solving your problem.
Mr_Spam

Re:placing string into DS:SI

Post by Mr_Spam »

OMG IT WORKS NOW!!!!! THANK YOU SO MUCH!!!!!!!

the big differance,

mov ax, cs
mov ds, ax

THANK YOU!!!!!

ive beeen breating my brains out over this for days!! thank you sooo much!!
Post Reply