I can't mix NASM and C

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
revival4u
Posts: 5
Joined: Mon Nov 26, 2007 3:16 am

I can't mix NASM and C

Post by revival4u »

this program is running in Linux
Using NASM assembler and GCC compiler


in "initSegs.inc"

Code: Select all

%define BOOT_SEG        0x07c0
%define KERNEL_SEG      0x1000

%define VIDEO_SEG       0xb800

%define FAT_SEG         0x0120
%define DIR_SEG         0x0240
%define LDR_SEG         0x0200

%define NUM_KERNEL_SECTOR               1
%define NUM_KROUTINE_SECTOR             1

SysCodeSelector         EQU             0x08
SysDataSelector         EQU             0x10
SysVideoSelector        EQU             0x18
;available aux
UsrCodeSelector         EQU             0x4b
UsrDataSelector         EQU             0x53

in "boot.asm"

Code: Select all

;============================================
;Boot records (FAT12 compatible)
;============================================

                jmp             boot_start

                db              90h

                db              'chos    '              ;need 8bytes
                dw              512                             ;byte per sector
                db              1                               ;sectors per sector
                dw              1                               ;reserved sectors
                db              2                               ;numbers of FAT
                dw              224                             ;entries of root directory
                dw              2880                    ;total sectors
                db              0f0h
                dw              9                               ;sector per FAT
                dw              18                              ;sectors per track
                dw              0                               ;numbers of hidden sectors
                dw              0
                dd              0
                db              0                            ; disk driver number(A:0, c:2)
                db              0
                db              029h
                dd              0
                db              'CHOS BIN  '
                db              'FAT12   '

                jmp             BOOT_SEG:boot_start

boot_start:
                cli
                mov             ax, cs
                mov             ds, ax
                mov             fs, ax
                mov             gs, ax
                mov             ss, ax

        .read_boot_sector
                mov             ax, KERNEL_SEG
                mov             es, ax
                mov             bx, 0x00

                mov             ah, 0x02
                mov             al, NUM_KERNEL_SECTOR
                mov             ch, 0x00
                mov             cl, 0x02
                mov             dh, 0x00
                mov             dl, 0x00
                int             0x13

                jc              .read_boot_sector

        .read_kRoutine
                mov             ax, 0x2000
                mov             es, ax
                mov             bx, 0x00

                mov             ah, 0x02
                mov             al, 0x01
                mov             ch, 0x00
                mov             cl, 0x03
                mov             dh, 0x00
                mov             dl, 0x00
                int             0x13

                ;jc             .read_kRoutine

                jmp             KERNEL_SEG:0x0000    ;0x1000:0x0000

                times   510-($-$$)      db      0
                dw              0xaa55
in "kernel.asm"

Code: Select all

%include "initSegs.inc"
[BITS 16]
[ORG 0h]

                jmp             boot_start

                db              90h

                db              'chos    '              ;need 8bytes
                dw              512                             ;byte per sector
                db              1                               ;sectors per sector
                dw              1                               ;reserved sectors
                db              2                               ;numbers of FAT
                dw              224                           ;entries of root directory
                dw              2880                    ;total sectors
                db              0f0h
                dw              9                              ;sector per FAT
                dw              18                              ;sectors per track
                dw              0                               ;numbers of hidden sectors
                dw              0
                dd              0
                db              0                           ; disk driver number(A:0, c:2)
                db              0
                db              029h
                dd              0
                db              'CHOS BIN  '
                db              'FAT12   '

                jmp             BOOT_SEG:boot_start

boot_start:
                cli
                mov             ax, cs
                mov             ds, ax
                mov             fs, ax
                mov             gs, ax
                mov             ss, ax

        .read_boot_sector
                mov             ax, KERNEL_SEG
                mov             es, ax
                mov             bx, 0x00

                mov             ah, 0x02
                mov             al, NUM_KERNEL_SECTOR
                mov             ch, 0x00
                mov             cl, 0x02
                mov             dh, 0x00
                mov             dl, 0x00
                int             0x13

                jc              .read_boot_sector

[b]        .read_kRoutine
                mov             ax, 0x2000
                mov             es, ax
                mov             bx, 0x00

                mov             ah, 0x02
                mov             al, 0x01
                mov             ch, 0x00
                mov             cl, 0x03
                mov             dh, 0x00
                mov             dl, 0x00
                int             0x13

                ;jc             .read_kRoutine
[/b]
                jmp             KERNEL_SEG:0x0000

                times   510-($-$$)      db      0
                dw              0xaa55

Code: Select all

%include "initSegs.inc"
[BITS 16]
[ORG 0h]

kernel_start:
                mov             ax, cs
                mov             ds, ax
                mov             es, ax
                mov             fs, ax
                mov             gs, ax
                mov             ss, ax

                lgdt[gdtr]

                mov             eax, cr0
                or              eax, 0x00000001
                mov             cr0, eax

                jmp             $+2
                nop
                nop

                jmp             dword   SysCodeSelector:pm_start


;====================================================
;--------------start of protected mode---------------
;====================================================

[BITS 32]

pm_start:
                mov             ax, SysDataSelector
                mov             ds, ax
                mov             es, ax
                mov             fs, ax
                mov             gs, ax
                mov             ss, ax

                lea             esp, [kernel_start]

                call    clear_scr
                call    print_kernel_msg

[b]                jmp             0x2000:0x0000[/b]

        .kernel_stop
                HLT
                jmp             .kernel_stop

clear_scr:              ;clear screen
        .proc_in
                push    eax
                push    ecx
                push    edi
                push    esi
                push    ds
                push    es
                pushfd

                ;get source addr [ds:esi]
                mov             ax, SysDataSelector
                mov             ds, ax
                mov             si, clear_scr.bg_color          ;get addr of .bg_color

                ;get target addr [es:edi]
                mov             ax, SysVideoSelector
                mov             es, ax
                xor             edi, edi

                ;copy source addr to target addr
                xor             ecx, ecx
                mov             ecx, 0x7ff

        .loop
                mov             ax, word [ds:esi]
                mov             word [es:edi], ax
                add             edi, 2
                dec             cx
                jnz             .loop

        .proc_out
                popfd
                pop             es
                pop             ds
                pop             esi
                pop             edi
                pop             ecx
                pop             eax
                ret

        .bg_color
                db              '.', 0x67


print_kernel_msg:
        .proc_in
                push    eax
                push    ebx
                push    edi
                push    esi
                push    es
                pushfd

                ;get source addr [esi]
                lea             esi, [.kernel_msg]
                mov             bl, byte [print_kernel_msg.bg_color]

                mov             ax, SysVideoSelector
                mov             es, ax
                xor             edi, edi

        .loop
                mov             al, byte [esi]
                or              al, al
                jz              .porc_out
                mov             byte [es:edi], al
                inc             esi
                inc             edi

                mov             byte [es:edi], bl
                inc             edi
                jmp             .loop

        .porc_out
                popfd
                pop             es
                pop             esi
                pop             edi
                pop             ebx
                pop             eax
                ret

        .kernel_msg
                db              'LOADING...', 0
        .bg_color
                db              '.', 0x23

;========================================
;--------------  G  D  T  ---------------
;========================================

gdtr:
                dw              gdt_end - gdt - 1               ;limit of GDT
                dd              gdt + 0x10000           ;base addr of GDT

gdt:
                ;NULL descriptor
                dd              0
                dd              0

                ;SysCodeSelector                EQU             0x08
                dw              0xffff
                dw              0x0000
                db              0x01
                db              0x9a
                db              0xcf
                db              0x00

                ;SysDataSelector                EQU             0x10
                dw              0xffff
                dw              0x0000
                db              0x01
                db              0x92
                db              0xcf
                db              0x00

                ;SysVideoSelector               EQU             0x18
                dw              0xffff
                dw              0x8000
                db              0x0b
                db              0x92
                db              0xcf
                db              0x00

                ;sys aux                EQU             0x20
                dd              0
                dd              0

                ;sys aux                EQU             0x28
                dd              0
                dd              0

                ;sys aux                EQU             0x30
                dd              0
                dd              0

                ;sys aux                EQU             0x38
                dd              0
                dd              0

                ;sys aux                EQU             0x40
                dd              0
                dd              0

                ;UsrCodeSelector                EQU             0x4b
                dw              0x00ff
                dw              0x0000
                db              0x10
                db              0xfa
                db              0xcf
                db              0x00

                ;UsrDataSelector                EQU             0x53
                dw              0x00ff
                dw              0x0000
                db              0x10
                db              0xf2
                db              0xcf
                db              0x00

                ;for usr aux

gdt_end:

                times   (512*NUM_KERNEL_SECTOR) -($-$$) db      0

in "kRoutineAsm.asm"

Code: Select all

[b]printk:
                push    ebp
                mov             ebp, esp

                push    eax
                push    edi
                push    es
                pushad

                mov             ax, SysVideoSelector
                mov             es, ax
                mov             edi, 0

                mov             byte [es:edi], 'H'
                inc             edi
                mov             byte [es:edi], 0x06
                inc             edi

                mov             byte [es:edi], 'i'
                inc             edi
                mov             byte [es:edi], 0x06
                inc             edi

                popad
                pop             es
                pop             edi
                pop             eax

                mov             esp, ebp
                pop             ebp

                ret[/b]

halt:
                HLT
                jmp             halt

                times   512 - ($-$$) db 0
in "kRoutine.c"

Code: Select all

#include <stdio.h>

int main(void){
        printk();
        halt();
}
in "Makefile"

Code: Select all

all: boot.asm kernel.asm kRoutineAsm.asm kRoutine.c
        nasm -f bin boot.asm -o boot.bin
        nasm -f bin kernel.asm -o kernel.bin
        gcc -c kRoutine.c
        nasm -f elf32 kRoutineAsm.asm
        ld -Ttext 0x00020000 -e main -o kRoutine kRoutine.o kRoutineAsm.o
        objcopy -R .note -R .comment -S -O binary kRoutine kRoutine.bin
        cat boot.bin kernel.bin kRoutine.bin > disk.img
I loaded 3rd sector to memory address of 0x2000:0x0000
and linking like above

but I can't see "Hi" in screen

what's wrong??

If you help me. I am really appreciate you

Thanks for your reading!!

Have a nice day[/code]
Last edited by revival4u on Mon Nov 26, 2007 3:59 am, edited 3 times in total.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

What is the error message - does it appear when linking? Does your compiler append underscores to function names? If so, you need to add an underscore to your assembly name.

http://www.osdev.org/wiki/How_To_Ask_Questions

Cheers,
Adam
revival4u
Posts: 5
Joined: Mon Nov 26, 2007 3:16 am

environment is Linux kernel version is 2.6

Post by revival4u »

because of runniung in linux and using GCC, don't need underscore~

and there is not any error or warning from compiler or assembler

this program just fall in panic!!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I can't find ANY cross-references between the ASM and C code - what is it that you actually try to do? (i.e. read AJs suggestion again. Thoroughly) :?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I loaded 3rd sector to memory address of 0x2000:0x0000
and linking like above

but I can't see "Hi" in screen

what's wrong??
But!... But!... That bears no resemblance whatsoever to your question title! How is not seeing correct text on a screen "not being able to mix nasm and C"? I don't understand, and why have you posted the entirety of your source code? I don't want to see all that! narrow it down!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

@James: the last 15 cm of text was edited in during my previous post. I think he attempted to fix his "asking questions" mistake.

@revival: editing your post can make the replies look meaningless. Will you please add a post next time instead of changing your original.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Sorry to appear to be flaming you, but could you also not add additional questions to your post after the post has been answered - it makes it look like Combuster and I missed something :wink: . Your original post ended after the "kRoutine.c" code snippet and had no further information. Instead, use 'Reply'.

Something else to try: Make your system data selector a flat 4GB selector and use offset 0xB8000 in to that. Does that now print on the screen? What about if, from your C code you declare an unsigned short pointer at 0xB8000 and write to that area? Note that C normally expects an unsegmented, flat memory model.

Cheers,
Adam
revival4u
Posts: 5
Joined: Mon Nov 26, 2007 3:16 am

Post by revival4u »

Im so sorry, everyone

I should remake this documents, and repost it

Thanks for all of your favors!!
Post Reply