flat assembler
Message board for the users of flat assembler.

Index > Main > Mixing assembly and C...

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
White-spirit: You have a stack imbalance.

For all your asm routines that are called from C, don't use "ret num", use only "ret".

For all your asm routines that call into C code use "add esp,number_of_parameters*4".

If you still can't see it, use a debugger to follow through and watch the stack to see what happens.
Post 05 Apr 2008, 16:09
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Thanks revolution .

I've added a prolog to my call_test function and it works ,at least, it doesn't reboot but the ( . ) is not printed on the screen :s

Here's the code :
Code:
format ms coff

public call_test

call_test:
push ebp
mov ebp, esp
sub esp, 4
push '.'
call putchar
add esp,4
leave
ret
    
Post 06 Apr 2008, 14:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
Why do you have "sub esp, 4", that is not needed
Post 06 Apr 2008, 14:57
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Without the "sub esp, 4" the kernel reboot, and I've found it here : http://www.milw0rm.com/papers/52 .
Post 06 Apr 2008, 15:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
White-spirit wrote:
Without the "sub esp, 4" the kernel reboot, and I've found it here : http://www.milw0rm.com/papers/52 .
I think you must have a major problem with your kernel. The "sub esp,4" is really not needed unless you are using that stack space for a local variable. But in your function you don't use it for local storage. And indeed the "leave" will restore the stack properly so whether you have the "sub esp,4" or not will make no difference at all to either your function or to the kernel. You could even put "sub esp,1000" and it wouldn't matter, it is just wasted stack space.
Post 06 Apr 2008, 16:09
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Can I post here my boot sector ( in asm ) and my kernel's ( C ) source code and my GCC and binutils version ?

Thanks
Post 06 Apr 2008, 16:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
White-spirit wrote:
Can I post here my boot sector ( in asm ) and my kernel's ( C ) source code and my GCC and binutils version ?
Well I expect the asm code will be acceptable, but this is not really the place for a kernel in C or binutils and things.

Have a search around here for other boot sectors people have posted previously. You might get some insight from reading how other approached the problem.
Post 06 Apr 2008, 16:19
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Okay thanks, here's my boot sector's code :
Code:
use16
org 0x7C00

;;;;;;;;; Loading the Kernel from floppy to 0x1000 ;;;;;;;;;
xor ax, ax                                ; ax <== 0
mov es, ax                             ; es <== ax <== 0
mov bx, 0x1000       ; destination adress
mov ah, 0x2                             ; function 02h ( reading sectors )
mov al, 0xE                               ; 14 sectors
xor ch, ch                              ;  ch <== 0 ( cylinder 0 )
mov cl, 0x2                            ; sector 2
xor dx,dx                         ; ( dh <== 0 , dl <== 0 )
int 0x13
;;;;;;;;; Memory infos ;;;;;;;;;
; coming soon ;

;;;;;;;;; Entering Protected Mode ;;;;;;;;;
cli                                            ; disables interrupts
xor ax,ax                              ; ax <== 0
mov ds,ax                              ; ds <== 0
lgdt [gdt_desc]
mov eax,cr0                         ; eax <== cr0
xor eax,eax
inc eax                                      ; eax <== 1
mov cr0,eax                           ; cr0 <== 1

;;;;;;;;; Jumping to Kernel ;;;;;;;;;
jmp dword 0x8:jmp_kernel
use32                                    ; using 32 bits instructions
jmp_kernel:
mov ax,0x10                          ; ax <== 10h
mov ds,ax                            ; ds <== 10h
mov ss,ax                            ; ss <== 10h
mov es,ax                            ; es <== 10h
mov fs,ax                            ; fs <== 10h
mov gs,ax                            ; eg <== 10h
mov esp, 0x90000             ; sets stack at 090000h
finit                                        ; initializing FPU registers
jmp dword 0x8:0x1000
jmp $

;;;;;;;;; Initializing GDT ;;;;;;;;;

gdt:

gdt_null:

gdt_desc:
     dw gdt_end - gdt - 0x1
      dd gdt
      dw 0x0

gdt_code:
 dw 0xFFFF
   dw 0x0
      db 0x0
      db 0x9A
     db 0xCF
     db 0x0
      
gdt_data:
   dw 0xFFFF
   dw 0x0
      db 0x0
      db 0x92
     db 0xCF
     db 0x0
      
gdt_end:
    
times 510-($-$$) db 0x90    ; Fills the file with Nop's
dw 0xAA55                                       ; MBR signature
    
Post 06 Apr 2008, 16:34
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Hello Wink
Code:
;;;;;;;;; Initializing GDT ;;;;;;;;; 
align 16
gdt: 

gdt_null: 
        dw 0x0
        dw 0x0
        dw 0x0 
        dw 0x0

gdt_code: 
        dw 0xFFFF 
        dw 0x0 
        db 0x0 
        db 0x9A 
        db 0xCF 
        db 0x0 
         
gdt_data: 
        dw 0xFFFF 
        dw 0x0 
        db 0x0 
        db 0x92 
        db 0xCF 
        db 0x0 
gdt_end: 
gdt_desc: 
        dw gdt_end - gdt - 0x1 
        dd gdt      
Code:
;;;;;;;;; Entering Protected Mode ;;;;;;;;; 
cli                                             ; disables interrupts 
xor ax,ax                               ; ax <== 0 
mov ds,ax                               ; ds <== 0 
lgdt [gdt_desc] 
mov eax,cr0                             ; eax <== cr0 
or eax,1                                 ; eax <== 1 
mov cr0,eax                             ; cr0 <== 1     
Post 06 Apr 2008, 19:23
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Thanks but it's still the same :s
Post 07 Apr 2008, 07:44
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
Code:
mov ax, 100h                              ; ax <== 100h 
mov es, ax                              ; es <== ax <== 100h 
xor bx,bx        ; sometimes, it bugs to put a non zero offset in BX.
mov ah, 0x2                             ; function 02h ( reading sectors ) 
mov al, 0xE                             ; 14 sectors 
xor ch, ch                              ;  ch <== 0 ( cylinder 0 ) 
mov cl, 0x2                             ; sector 2 
xor dx,dx                               ; ( dh <== 0 , dl <== 0 ) 
int 0x13 
    


are you sure the kernel is orged at 1000h ???

Mods: note you can move this thread in OS construction.
Post 07 Apr 2008, 08:34
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Otherwise the kernel would not have shown messages on the screen before restarting, right?

PS : I thought it was juste a problem with linking the MS COFF object using ld so I asked here .
Post 07 Apr 2008, 10:44
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Yeah, i used "format elf" instead of MS COFF and it works =')
Post 07 Apr 2008, 12:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.