printing problem

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.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

printing problem

Post 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.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: printing problem

Post 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.
Last edited by ru2aqare on Sat Dec 06, 2008 2:20 pm, edited 1 time in total.
User avatar
Blue
Member
Member
Posts: 31
Joined: Thu Aug 02, 2007 6:34 am
Location: on the stack

Re: printing problem

Post 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
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post 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?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: printing problem

Post 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.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post 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!
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: printing problem

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post by VolTeK »

ok, so it will stop printing letters when the pointer his 0?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: printing problem

Post by Troy Martin »

Yes, that's it.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post by VolTeK »

ok, thanks
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: printing problem

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post 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.
User avatar
stephenj
Member
Member
Posts: 140
Joined: Wed Jul 23, 2008 1:37 am
Location: Canada

Re: printing problem

Post 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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: printing problem

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: printing problem

Post 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?
Locked