Page 1 of 2

printing problem

Posted: Sat Dec 06, 2008 2:02 pm
by VolTeK
maybe its because im missing something, but when i use a printing function from any tutorial it alwayscomes out wrong.

when i do something like mov si, hello
call print



hello db 'Hello' ,0 <what is the ,0 for? any way

i always get

Hello☻23☺♣☻_

what is my problem, what am i missing, or could you just give me a coding example? i use fasm on an x86 (amd 64) machine.

Re: printing problem

Posted: Sat Dec 06, 2008 2:20 pm
by ru2aqare
GhostXoPCorp wrote: hello db 'Hello' ,0 <what is the ,0 for? any way
Most string output routines expect a C-style zero terminated string. In assembly that looks something like this:

Code: Select all

   mov  si, offset string_to_print ; make sure ds: is set to the same segment as cs
   call print
   ...

; subroutine expecting ds:si with address of string to print using BIOS.
print proc near
loop:
   mov  al, ds:[si] ; or use lodsb, or use mov al, cs:[si]
   test al, al
   je   exit
   pusha
   mov  ah, 0Eh ; load BIOS function number, this may be the wrong number, I have switched to C a long time ago
   mov  bx, 7 ; 07: grey text on black background
   int  10h
   popa ; restore registers, some BIOSes may screw si and/or di
   inc  si ; move to next byte
   jmp short loop
exit:
   retn
print endp
Other than that, I have no idea why your code does not work.

Re: printing problem

Posted: Sat Dec 06, 2008 2:20 pm
by Blue
Hi,
GhostXoPCorp wrote:hello db 'Hello' ,0 <what is the ,0 for? any way
The 0(NULL terminator) is used to mark the end of the string, and it is what you need to check for in order to tell when the string ends. Judging by your problem it seems like you either don't check for the NULL terminator or you check for something else. But it is not really easy to tell without seing your code.

Basically you need to check every character you get, if it is not NULL then you print it, and if is NULL you stop printing and return. The simple example I learned some time ago when I started on assembly is:

Code: Select all

print:                      
; This code assumes that DS:SI is pointing to a NULL terminated string
    push    ax          ; Save AX in case changing it will mess up anything 
    mov     ah, 0x0E    ; Set AH to 0x0E, because we will be using teletype output
    
@@:                     ; The main loop
    lodsb               ; Load the next byte from DS:SI into AL and update SI
    cmp     al, 0x00    ; Is is NULL (Have we reached the end of the string)?
    je      @f          ; YES? Then exit the loop
    int     0x10        ; NO? Then print it using BIOS function 0x10 AH=0x0E
    jmp     @r          ; Continue in our loop
    
@@:                     ; Leaving the main loop
    pop     ax          ; Restore AX, because we pushed it to the stack earlier
    ret                 ; Return to the calling code
The above code is to be used with FASM or compatible, and it assumes that you are not in protected mode since it uses BIOS interrupts. If you are in protected mode then it is a bit more advanced, but instead of int 0x10 you need to have a memory offset and update this (or x and y coordinates and calculate the memory offset from this).

I hope it helps, else I guess we will have to see some code.

Blue

Re: printing problem

Posted: Sat Dec 06, 2008 2:23 pm
by VolTeK
ok, im using real mode, what do i have to do to get that code to work, will it work independantly or will i have to set something up for that to work?

Re: printing problem

Posted: Sat Dec 06, 2008 2:24 pm
by ru2aqare
GhostXoPCorp wrote:ok, im using real mode, what do i have to do to get that code to work, will it work independantly or will i have to set something up for that to work?
Both pieces of code should work out of the box.

Re: printing problem

Posted: Sat Dec 06, 2008 2:30 pm
by VolTeK
ok so it will work if i attch it to

org 7c00h

bpb

print code<

it will work fine?, other than that, what does

Hello db 'hello' ,0 what does ,0 mean?

my last question!

Re: printing problem

Posted: Sat Dec 06, 2008 2:46 pm
by Troy Martin
Like we said before, the ,0 is the null terminator for the print routine (which you use by doing a call print) to stop printing at.

Re: printing problem

Posted: Sat Dec 06, 2008 2:48 pm
by VolTeK
ok, so it will stop printing letters when the pointer his 0?

Re: printing problem

Posted: Sat Dec 06, 2008 2:51 pm
by Troy Martin
Yes, that's it.

Re: printing problem

Posted: Sat Dec 06, 2008 9:04 pm
by VolTeK
ok, thanks

Re: printing problem

Posted: Sun Dec 07, 2008 10:49 am
by Combuster
Another person who might better try doing userland programming properly before jumping to OS development. (given that you don't now about null-terminated strings and can't deduce a piece of code's behaviour)

Re: printing problem

Posted: Sun Dec 07, 2008 12:37 pm
by VolTeK
i know alot about how operating systems work, i just started learning assembly code thats all. i know all of the registers its just inturrupts i need to know, (my os will be rmode only) and it will be in tui. pretty much just processsor stuff i have to look at. well the important stuff. yes this is my first, just some things i want to know before i actually get started.

Re: printing problem

Posted: Sun Dec 07, 2008 1:00 pm
by stephenj
GhostXoPCorp wrote:i know alot about how operating systems work...
I know a lot about the human body, I've been using one all my life! I even have decades of experience maintaining one.

Nevermind that I've never studied biology. But I have read WebMD once.

Well, I'm off to the doctor's office to give them my latest prognosis! I hope they don't have the audacity to disagree with me again.

Re: printing problem

Posted: Sun Dec 07, 2008 3:12 pm
by Troy Martin
GhostXoPCorp wrote:i know alot about how operating systems work, i just started learning assembly code thats all. i know all of the registers its just inturrupts i need to know, (my os will be rmode only) and it will be in tui. pretty much just processsor stuff i have to look at. well the important stuff. yes this is my first, just some things i want to know before i actually get started.
Real mode assembly bare bones - I wrote that tutorial.

Re: printing problem

Posted: Mon Dec 08, 2008 1:45 pm
by VolTeK
thank you very much, by the way, the printing did not work, i could only get it to print one letter. from the back of my head, here is the code i used

org 100h
use16
hello db 'Hello',0

print:
; This code assumes that DS:SI is pointing to a NULL terminated string
push ax ; Save AX in case changing it will mess up anything
mov ah, 0x0E ; Set AH to 0x0E, because we will be using teletype output

@@: ; The main loop
lodsb ; Load the next byte from DS:SI into AL and update SI
cmp al, 0x00 ; Is is NULL (Have we reached the end of the string)?
je @f ; YES? Then exit the loop
int 0x10 ; NO? Then print it using BIOS function 0x10 AH=0x0E
jmp @r ; Continue in our loop

@@: ; Leaving the main loop
pop ax ; Restore AX, because we pushed it to the stack earlier
ret ; Return to the calling code


mov si, hello
call print

with Hello it prints <"||pk"

with hello = h
it will print h only
can you modify my code a littlebit to help me please?