printing problem
printing problem
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.
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
Most string output routines expect a C-style zero terminated string. In assembly that looks something like this:GhostXoPCorp wrote: hello db 'Hello' ,0 <what is the ,0 for? any way
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
Last edited by ru2aqare on Sat Dec 06, 2008 2:20 pm, edited 1 time in total.
Re: printing problem
Hi,
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:
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
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.GhostXoPCorp wrote:hello db 'Hello' ,0 <what is the ,0 for? any way
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
I hope it helps, else I guess we will have to see some code.
Blue
Re: printing problem
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
Both pieces of code should work out of the box.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?
Re: printing problem
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!
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!
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: printing problem
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
ok, so it will stop printing letters when the pointer his 0?
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: printing problem
Yes, that's it.
Re: printing problem
ok, thanks
- Combuster
- 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
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
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
I know a lot about the human body, I've been using one all my life! I even have decades of experience maintaining one.GhostXoPCorp wrote:i know alot about how operating systems work...
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.
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: printing problem
Real mode assembly bare bones - I wrote that tutorial.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.
Re: printing problem
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?
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?