printing error!

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.
Post Reply
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

printing error!

Post 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!!!:?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: printing error!

Post by AJ »

Hi,
olainen wrote:

Code: Select all

mov ax, 0x7C00
  mov ds, ax
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
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

Re: printing error!

Post 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.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: printing error!

Post by DavidCooper »

Code: Select all

boot:
  mov ax, 0x7C00
  add ax, 544
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.

Code: Select all

do:
  mov si, string
  call print

  hlt
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.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

Re: printing error!

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: printing error!

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: printing error!

Post 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
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

Re: printing error!

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

Re: printing error!

Post 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.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: printing error!

Post 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).
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: printing error!

Post 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 :roll: )
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply