linking asm 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
mercury

linking asm and C

Post by mercury »

I typed some code in asm language that defined a procedure getc and clearscreen
[bits 32]

GLOBAL _main
GLOBAL _clearscreen

SECTION .text

_getc: mov ah, 0
int 16h
ret

_clearscreen: mov al, 3
mov ah, 0
int 0x10
ret

I then typed in this C code,
extern void getc(void);
extern void clearscreen(void);

int main(void) {
clearscreen();
getc();
}

compiled both into -o
tried linking with ld -Ttext 0x100000 --oformat binary -o kernel32.bin mix_c.o mix_asm.o and got mix_c.o(.text+0x11):mix_c.c: undefined reference to `_getc'
why? If anybody can help, I would greatly appreciate it.
DarylD

RE:linking asm and C

Post by DarylD »

Unfortunately I use gas rather than NASM, but I think what you are missing is some statement in the asm file declaring the functions to be global, for example in gas I would write:

.globl _getc
_getc: ...

I am sure somebody on here could tell you the nasm way of doing it.
Mercury

RE:RE:linking asm and C

Post by Mercury »

I did that already. It's not not accessing it properly. I guess it could be something with my linker. I use DJGPP. Thanks for taking a look. My OS is going to take a lot of work if I ever even get it started :-/
Mercury

RE:RE:linking asm and C

Post by Mercury »

I did that already. It's not not accessing it properly. I guess it could be something with my linker. I use DJGPP. Thanks for taking a look. My OS is going to take a lot of work if I ever even get it started :-/
fixa

RE:linking asm and C

Post by fixa »

in gcc it should be
file.ASM:

.text
.globl getc
getc:
...
ret

.globl clearscreen
clear...:
...
ret

file.C:
extern void getc(void);
extern void clearscreen(void);

int main....

It works in my OS
mercury

RE:linking asm and C

Post by mercury »

Well, I've managed to get the linking out of the way, I think. Now I have some new problems. Um, I think I wrote them in another message on here. Basically I set a pointer(in c) to point to the beginning of the video memory, 0xB8000 I think, I don't have the code at hand, then say *pointer = *character and bingo, character is written to the screen, however,
mov [0xB8000], 100(put d to screen) and anything like that, just causes the computer to reboot O_o

It's not a protected mode/real mode thing, it can't be because the C code works fine in real mode, besides the fact that I can't figure out how to set up protected mode, I'll have to do that though once I start really working on this stuff.
Post Reply