Author Topic: Weird problem with "call"  (Read 11776 times)

Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Weird problem with "call"
« on: February 17, 2012, 09:27:56 PM »
Hi all :)

I'm have a big problem with "call".
I'm writing a funtion to draw a line (for my OS), it's absolutely empty but I can "call" it only 13 times.
Code: [Select]
DrawLine:
dl_start:
     ; just empty
ret

; ...

call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine
call DrawLine

; ...
If I call it the 14th time it crashes and stops.
Does anyone know why this happens?

Thanks in advance!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #1 on: February 17, 2012, 11:29:48 PM »
I don't see anything in what you show that could be a problem. Could it be the size of the code, rather than the number of call instructions? (trying to run more code than you've loaded, or some such?) As a "debugging experiment", can you call your empty function 14(+) times in a loop, rather than with multiple "inline" calls?

I'm sure it doesn't matter, but I'm curious how you happened to discover this problem. Why are you calling an empty function 14 times? I suppose it wasn't empty when you first noticed it...

I've read generic debugging advice that says, "If your program doesn't work, something you believe to be true is not true." Now you just have to figure out what that is! :) Since I can't see anything wrong here, I suspect the problem must be elsewhere...

Very strange! If you can't figure it out, give us more "context"...

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #2 on: February 17, 2012, 11:57:46 PM »
Hi,

You're right, my function wasn't empty before but I wanted to check out if the code in the function is the reason. I forgot to tell you one really important thing. I have a boot.bin and a kernel.bin file. I want to copy them (with the cmd copy function) to an *.img. I had often the problem that a huge part of the kernel.bin wasn't copied to the *.img until I copied it binary -> copy /b boot.bin + kernel.bin OS.img. There wasn't the "call problem" before. Maybe the reason isn't Assembler (there are no errors and warning) but the copying. It is an Operation System and not an application and I don't have a lot of experience...
I'm gonna try your proposes. :)

Thanks!

EDIT:
The size of the application is always the same because of
Code: [Select]
times 1228247-($-$$)-2 db 0
« Last Edit: February 18, 2012, 10:00:09 AM by isywum »

Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #3 on: February 18, 2012, 10:20:48 AM »
Well, I tried this code
Code: [Select]
mov cx, 0
calltest:
call dl_DrawLine
inc cx
cmp cx, 9999
je ende
jne calltest
and it works! But it doesn't help me because I can't use this function like this. :D
Do you have an idea why the first way dosn't work?

Thanks!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #4 on: February 18, 2012, 03:15:23 PM »
Size. You copy boot.bin and kernel.bin to os.img. In your boot.bin, you read some number of sectors into memory, presumably. How many? Enough? Have you got enough memory there to fit without overwriting anything? That's the kind of thing I'd be looking at. Hard to say without seeing the whole setup...

If it wuz easy, everybody'd be doing it! :)

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #5 on: February 19, 2012, 11:41:15 AM »
Hi,

I didn't write the boot.asm myself. :( I just copied it and hoped I'd understand the code later.
I can show you my boot.asm
Code: [Select]
org 0x7C00

start:
cli
mov ax, 0x9000
mov ss, ax
mov sp, 0
sti

mov [bootdriv], dl

call load
mov ax, 0x1000
mov es, ax
mov ds, ax
push ax
mov ax, 0
push ax
retf

bootdriv db 0
loadmsg db "Starting...", 13, 10, 0
 
putstr:
lodsb
or al, al
jz short putstrd
mov ah, 0x0E
mov bx, 0x0007
int 0x10
jmp putstr

putstrd:
retn
 
load:
push ds
mov ax, 0
mov dl, [bootdriv]
int 0x13
pop ds
jc load
 
load1:
mov ax, 0x1000
mov es, ax
mov bx, 0
mov ah, 2
mov al, 5
mov cx, 2
mov dx, 0
int 0x13
jc load1
mov si, loadmsg
call putstr
retn
 
times 512-($-$$)-2 db 0
dw 0AA55h
I hope it helps. :)

Thanks!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #6 on: February 19, 2012, 02:25:00 PM »
Well, it would probably help if you understood what you were doing, yeah. :)

We can go through the entire bootsector if you want, but the (possibly) relevant part is in the "load" routine. Int 13h, with 2 in ah, reads the number of sectors in al (into memory at es:bx) - so 5, in this case. At 512 bytes per sector, that comes to 2560 bytes. If the "14th call" puts your kernel over that value, you've probably found the problem - increase the value in al. You can't increase it without limit, though. The "typical" floppy (anything you're likely to find or emulate) has 18 sectors per track. Beyond that, you'll be on "head 1" (the number in dh). The actual arithmetic for int 13h is rather ugly - that's the simplified version. Just increasing al by a few more sectors will probably give you enough room to play in, before you get into the details...

If your kernel isn't over 2560 bytes, then we haven't found the problem yet, and will have to keep looking, but I suspect that's what you've run into...

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #7 on: February 19, 2012, 04:05:57 PM »
Thank you very much for your "code descripton". I'm starting understaning the code. :)
Unfortunaly it doesn't help me with my problem. The size of my kernel is always 1.228.245 bytes because of
Code: [Select]
times 1228247-($-$$)-2 db 0
Do you know a good book or tutorial about all these things (bootloader etc.)? It might help me understanding. :)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #8 on: February 19, 2012, 04:33:18 PM »
You've got a serious problem, since your kernel won't fit in real-mode memory... :)

Try http://www.osdev.org - loads of good info there. The "baby steps" tutorial is pretty good for understanding a bootsector, as I recall...

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #9 on: February 19, 2012, 06:23:06 PM »
Hi,

thank you for this great link. :)
I have just a little question.
Quote
since your kernel won't fit in real-mode memory...
Can you tell me what you mean exactly by this? I unfortunately don't unterstand it (maybe because of my bad English :D).

Thanks!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #10 on: February 20, 2012, 07:46:42 AM »
Simply that you can only address 1 megabyte of memory in real mode...

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #11 on: February 20, 2012, 10:57:11 AM »
So, I have to use the pm, right? Could that be the reason of my problem?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Weird problem with "call"
« Reply #12 on: February 20, 2012, 11:19:51 AM »
I imagine you'll want to switch to pm, if you haven't. BIOS interrupts won't work in 32-bit code, of course. I'm guessing that your problem is that you're not loading "enough". You might want to try commenting out the "padding" line and assembling to see how big your kernel "really" is. Make sure you're loading "enough" sectors. Then uncomment the "padding" so your ".img" is the right size (I assume that's the purpose?).

I got as far as booting and loading some more sectors, then realized that I had no clear idea what I wanted to load that would be "better" than Windows/Linux/etc. If I ever figure that out I might get back to it. A little late in life for me to be starting an OS! Fun(?) and educational to play with a "toy OS", though...

Best,
Frank


Offline isywum

  • Jr. Member
  • *
  • Posts: 38
Re: Weird problem with "call"
« Reply #13 on: February 20, 2012, 11:43:15 PM »
Hi,

I'm absolutely confused. I wanted to find out how big my kernel actually is (309 bytes) and tried "calling" more than 13 times again and it suddendly worked! I just changed the position of some variables in my code... I can't imagine that that's the reason of my problem. :D

I'm just a 13 years old german boy - so I still have some time :) - and progam just for fun, however, I love programming. :) I hope there won't be this problem starting form now. :D

Thank you very much for your help. This forum is just great. :)