Page 1 of 1
printing error!
Posted: Wed Jan 25, 2012 10:56 am
by huh
something went wrong, here is my asm (bootloader = the whole os) code:
Code: Select all
bits 16
org 0x7C00
section .text
boot:
mov ax, 0x7C00
add ax, 544
cli
mov ss, ax
mov sp, 0x4096
sti
mov ax, 0x7C00
mov ds, ax
mov byte [bootdev], dl
jmp ajust
ajust:
mov eax, 0
jmp do
do:
mov si, string
call print
hlt
;======================== functions
print:
pusha
mov ah, 0x0E
.print_1:
lodsb
cmp al, 0
je .print_done
int 0x10
jmp .print_1
.print_done:
popa
ret
string: db 'HEY!!!', 0
;======================== sub functions
bootdev: db 0
for_nasm:
times 510-($-$$) db 0
dw 0x0AA55
buffer:
When I run it in QEMU it does nothing!
HUH!!!:?
Re: printing error!
Posted: Wed Jan 25, 2012 11:01 am
by AJ
Hi,
To start with, take a look at how you set your segment registers. There are possibly other errors in there too, but that's as far as I got.
Cheers,
Adam
Re: printing error!
Posted: Wed Jan 25, 2012 11:09 am
by huh
I tried to move a character directly to al instead of loding the string: It worked. But loading the string with lodsb just does not work. So I guess it boots correctly.
Re: printing error!
Posted: Wed Jan 25, 2012 1:50 pm
by DavidCooper
Comment your code so that people can understand the purpose of the 544.
Code: Select all
cli
mov ss, ax
mov sp, 0x4096
sti
You could probably think of a better place to put your stack, though it may not matter.
Code: Select all
mov ax, 0x7C00
mov ds, ax
mov byte [bootdev], dl
jmp ajust
ajust:
mov eax, 0
jmp do
Saving dl is sensible, but you should comment your code to say why. Don't know why you're putting in the unnecessary jumps, or why you feel the need to load eax at this stage.
It looks as all the above code should work though, assuming the assembler expects ds to contain 7c00 when it calculates the address for the string - I don't know enough about assembler to tell you if there's a problem there.
Code: Select all
print:
pusha
mov ah, 0x0E
.print_1:
lodsb
cmp al, 0
je .print_done
int 0x10
jmp .print_1
.print_done:
popa
ret
The int 0x10 may corrupt si, so try pushing si before int 0x10 and popping it afterwards. It isn't beyond possibility that the BIOS could corrupt ds as well, so you might want to reload that each time too. I've never used the BIOS to do printing so I don't know if you're doing it the right way, but you probably are as you say it works when you load al directly with a char value. If you want to try printing direct to the screen without going through the BIOS, load es with b800 and use stosw to send char vals to even addressses (starting at 0) and colour values to the odd addresses following them.
Re: printing error!
Posted: Wed Jan 25, 2012 1:56 pm
by huh
everything from the boot: label is taken from another os. the print routine is also taken from an other os. it worked for that os (i tried it) but it does not work here. I use nasm assembler.
thanks, i will try that.
Re: printing error!
Posted: Wed Jan 25, 2012 2:05 pm
by AJ
Hi,
olainen wrote:I tried to move a character directly to al instead of loding the string: It worked. But loading the string with lodsb just does not work. So I guess it boots correctly.
Yes, but have you fixed the problem with your segment register values being out by a factor of 16?
Also when the crash occurs, what does the Qemu register output look like? Are CS:IP and SS:SP where you would expect them to be (sure I've posted this comment already today)? Is there a reason you add 544 (0x220) to the value of SS? The readability is awful, why are some numbers in hex added to others in decimal? Why (twice) do you do an unconditional near jump to the next line of code?
Cheers,
Adam
Re: printing error!
Posted: Wed Jan 25, 2012 2:08 pm
by AJ
Hi,
Sorry for stopping the flow of the topic - that's the danger of going away from the pc for 10 minutes - 2 replies had been posted before I finished mine!
olainen wrote:everything from the boot: label is taken from another os. the print routine is also taken from an other os. it worked for that os (i tried it) but it does not work here. I use nasm assembler.
thanks, i will try that.
The danger of copy-paste coding (apart from the fact that you do not learn anything) is that everything is out of context. Research, understand and then write it yourself.
Cheers,
Adam
Re: printing error!
Posted: Wed Jan 25, 2012 2:43 pm
by huh
Hi. i tried to do this instead of moving 0x7C00 to ax and adding 544:
Code: Select all
mov ax, cs ; code section = this bootloader
mov ds, ax ; data section = code section so I can call print routine
AND IT WORKED!!!
Re: printing error!
Posted: Wed Jan 25, 2012 3:06 pm
by VolTeK
If it took you long enough to find out it was a segment problem, please find out how to use a debugger and soon.
Re: printing error!
Posted: Wed Jan 25, 2012 4:05 pm
by DavidCooper
olainen wrote:Hi. i tried to do this instead of moving 0x7C00 to ax and adding 544:
Code: Select all
mov ax, cs ; code section = this bootloader
mov ds, ax ; data section = code section so I can call print routine
AND IT WORKED!!!
I wouldn't get too excited about that - you can't guarantee that cs holds the right value as it will be different on some machines. You'd be better off making the correction that AJ pointed you towards (and which I missed before, despite reading his post - 7C00 looked right to me because it is right as an address, but it's well wrong when it's in a segment register: you want 7C0).
Re: printing error!
Posted: Wed Jan 25, 2012 5:02 pm
by bubach
IIRC you should make sure both CS and DS contain 0x0000 with the org at 0x7c00, or use "org 0" instead. check the org and DS values on the bootloader you snatched the source from
It sounded like your DS got the wrong value based on the inability to read the string var. So your fix also confirms this, but you could do a jmp 0x0000:start thing to _really_ make sure CS is correct before copying it's value to DS. Just to be save.
(hoping not to be talking out of my @$$ here, been a while since i touched os-dev/asm
)