Page 1 of 1

What's wrong in this ?

Posted: Wed Jul 23, 2003 11:00 pm
by modshah
I do not get any compile or link error but when it comes to execution - The program does print 'Hello World' but then it dumps a stack trace on the screen.

Stack Trace with output 'Hello World':

Hello WorldExiting due to signal SIGSEGV
General Protection Fault at eip=0000160a
eax=00000dff ebx=000001c1 ecx=00000000 edx=0000033f esi=0000160e edi=0000d762
ebp=0008d718 esp=0008d718 program=F:\OS\SOURCE\PRINT\CALLPR~1.EXE
cs: sel=01a7  base=01c10000  limit=0009ffff
ds: sel=01af  base=01c10000  limit=0009ffff
es: sel=01af  base=01c10000  limit=0009ffff
fs: sel=017f  base=00005c10  limit=0000ffff
gs: sel=01bf  base=00000000  limit=0010ffff
ss: sel=01af  base=01c10000  limit=0009ffff
App stack: [0008d760..0000d760]  Exceptn stack: [0000d6c0..0000b780]

Call frame traceback EIPs:
  0x0000160a
  0x0000162e
  0x00002e88
----------------------------------------------------------------------

Source Code:

//C Code to call print defined in ASM
#include<stdio.h>

int main()
{
char *s = "Hello World";
print(s);
}

//ASM code to print
global _print

SEGMENT .text
[BITS 32]

_print:
        push ebp
        mov ebp, esp
        mov esi,[ebp+8]
.continue
        lodsb
        mov ah,0x0E
        or al,al
        jz .end
        int 0x10
        jmp .continue
.end

RE:What's wrong in this ?

Posted: Wed Jul 23, 2003 11:00 pm
by VE3MTM
You are trying to call a BIOS interrupt, and your process is running in v86 mode.

If you were to dump this to a bootdisk and run it in protected mode, then it would (probably) work fine.

RE:What's wrong in this ?

Posted: Wed Jul 23, 2003 11:00 pm
by Jamethiel
Where's the mov esp, ebp / pop ebp / ret at the end of your ASM routine?

RE:What's wrong in this ?

Posted: Wed Jul 23, 2003 11:00 pm
by TripleFault
What assembler are you using to compile the source code?  I've noticed that this sometimes happens to me when I use MS-DOS debug but not when I use a normal assembler.
~ TripleFault !)

RE:What's wrong in this ?

Posted: Thu Jul 24, 2003 11:00 pm
by modshah
hi everybody. Dear Jamethiel you are perfectly right.

I did a stupid thing. Actually if you could see the msg board - I had asked help on passing pointer values to asm code and with help from you all, I figured out a way(I'm really greatful to u all). I was a bit too excited and wanted to immediately try passing strings. And in the hurry I didn't add the following code:

mov esp, ebp / pop ebp / ret

Anyway friends - Now that I have added it- It works Fine.
Here is the code. The assembler that I use: NASM (info for


//ASM Code
global _print

SEGMENT .text
[BITS 32]

_print:
        push ebp
        mov ebp, esp
        mov esi,[ebp+8]
.continue
        lodsb
        mov ah,0x0E
        or al,al
        jz .end
        int 0x10
        jmp .continue
.end
        pop ebp
        ret

//C Code
#include<stdio.h>

int main()
{
char *s = "Hello World";
print(s);
}

Thanks again
modshah

PS: I am really happy to join this community. Though at the moment, I am just a beginner, in future I will be a contributor. Once again thanks to all those selfless soles out there who help us (newbies).

RE:What's wrong in this ?

Posted: Thu Jul 24, 2003 11:00 pm
by modshah
Dear TripleFault, actually there was no problem with the assembler. Actually it was my mistake(please see the last post from me in this msg thread). I had not coded the 'ret' in the procedure call.

As for the assembler, I use NASM.

Thanks
modshah

RE:What's wrong in this ?

Posted: Sun Jul 27, 2003 11:00 pm
by rexlunae
Aside from the bug that you found earlier, I see another problem here.  You must be running in real mode (you are using a BIOS call), so your assembly code should be BITS 16, not BITS 32.  I don't think that for this function that will cause a problem, but it might for other functions.  It seems to me that because NASM thinks you are in a 32-bit segment, it does not insert the overrides, so when you code 'push ebp' it is actually outputting the 16-bit version 'push bp', and likewise for the other lines.

That is, unless I'm missing something here...