Home arrow Forums
OSDEV Forums  


Spectre
User

Junior Osdever
Posts: 13
graphgraph
Karma: 0  
Printing strings - 2004/10/24 03:20 I'm working on a bootloader and it has a simple procedure to print characters:

printchar:

lodsb
or al,al
je hang
mov ah, 0x0E
mov bh, 0x00
mov bl, 7
int 0x10
jmp printchar

Whatever si points to gets printed as long as its not 0. The problem is i get junk after whatever it prints. Is there a command that can find the length of strings so the function knows when to stop? :?
<span class='smallblacktext'>[ Edited ]</span>
  | | The administrator has disabled public write access.
OSDEV
Community
Advertisement
   
DRF
User

Platinum Osdever
Posts: 123
graphgraph
Karma: 1  
Re: Printing strings - 2004/10/24 12:13 Ok, to optimise your existing code you can remove the text setup stuff so having the loop after the mov ah , bh and bl commands as long as you remember not to change those registers within the loop (otherwise your moving stuff to the registers each time you print a char that isn't needed but at this level it won't matter)

Also the jump command is probably what is causing the problem, the command I use is 'jz' Jump if Zero. So if you OR the values you should get: 0 OR 0 = 0 (anything else is 1) so the result is obviously if you pass a 0 into al, zero. I'm not sure (without having a reference manual to hand) how that OR instruction effects the equal flag to know if the je will work or not but logically the jz is a better choice I would suggest.

The procedure I came up with for string printing is: (pretty much cut and paste from one of my documents)
Code:

PutStr: ; Procedure label/start
; Set up the registers for the interrupt call

mov ah,0x0E ; The function to display a chacter (teletype)
mov bh,0x00 ; Page number
mov bl,0x07 ; Normal text attribute (White text black back ground, change value for random colours)

.nextchar ; Internal label (needed to loop round for the next character)
lodsb ; I think of this as LOaD String Block
; (Not sure if thats the real meaning though)
; Loads [SI] into AL and increases SI by one
; Check for end of string '0'

or al,al ; Sets the zero flag if al = 0
; (OR outputs 0's where there is a zero bit in the register)

jz .return ; If the zero flag has been set go to the end of the procedure.
; Zero flag gets set when an instruction returns 0 as the answer.

int 0x10 ; Run the BIOS video interrupt
jmp .nextchar ; Loop back round to the top
.return ; Label at the end to jump to when complete
ret ; Return to main program

(I apologise for the hard to read code above the forum seems to not like me using indentations on code)
This would be executed by the command:
Code:

mov si, stringname
call PutStr

Where stringname is:
Code:

stringname db "Test String",0


Hope that helps, feel free to try an other solution but my print string commands there if you can't find another way yourself. But I think by changing the je to jz you may solve the problem. (Other useful additions to printing is that you can make a string as: "String",13,10,0 and the 13,10 are the ascii codes to start a new line if you want to print more than 1 thing)

In direct response to your question it is possible to count the characters in a string but it will be harder and very similar method that you might as well put the extra few lines of code in to print to screen at the same time.

If that doesn't help please reply with a quote of the message displayed on the screen and the method you use to call the function.

Daniel
  | | The administrator has disabled public write access.
Spectre
User

Junior Osdever
Posts: 13
graphgraph
Karma: 0  
Re: Printing strings - 2004/10/24 20:42 Thanks alot, it works now, I didn't realize the significance of putting ,0 after the string.
<span class='smallblacktext'>[ Edited Mon Oct 25 2004, 01:51AM ]</span>
  | | The administrator has disabled public write access.
DRF
User

Platinum Osdever
Posts: 123
graphgraph
Karma: 1  
Re: Printing strings - 2004/10/25 14:07 Good news that you got it working. All ways the simple things Some people use different ways to identify the end of a string but 0 is the easiest and most common.

Daniel
  | | The administrator has disabled public write access.

A WebArticles site. Sponsored by Evoleto. Motorola V525 / Business Directory / Delaware Incorporation / Home Made Bazaar