mixing c and assembly
Posted: Tue Aug 13, 2002 11:00 pm
I am trying to create a small bootsector using assembly language and C. I am working with the
NASM assembler on Red Hat Linux 7.2. I would like the boot sector to print the message 'Hi' to the
screen. Here is what my code looks like:
/* HELLO.C */
char H = 'H';
char I = 'i';
/* HELLO.S */
[SECTION .data]
extern H
extern I
[SECTION .text]
[GLOBAL _start]
_start
jmp bootup
bootup:
mov ax, 0xb800
mov es,ax
mov al, BYTE [H]
mov byte[es:0x0f9c] ,al
mov al, BYTE [I]
mov byte[es:0x0f9e], al
pause:
jmp pause
ret
times 510-($-$$) db 0
dw 0xAA55
After I have created the two files above, I first compile the hello.c file with the following command:
cc -c hello.c
this command creates the file 'hello.o' in the current directory. Next I assemble the hello. s file with
the following command:
nasm -f aout -o hello_asm.o hello.s
This command creates the file 'hello_asm.o' in the current directory. Next I link the hello.o and
hello_asm.o into a file called hello.bin with the following command:
ld -o hello.bin -oformat binary -Ttext=0x00 hello.o hello_asm.o
This command creates the file 'hello.bin' into the current directory. Lastly, I copy the hello.bin binary
to the boot sector of a disk with the command:
dd if=hello.bin of=/dev/fd0 bs=512 count=1
When i reboot my system with the diskette in the disk drive I only get a blank screen. Can anyone
please help me with this problem.
Thanks
Michael Morrison
NASM assembler on Red Hat Linux 7.2. I would like the boot sector to print the message 'Hi' to the
screen. Here is what my code looks like:
/* HELLO.C */
char H = 'H';
char I = 'i';
/* HELLO.S */
[SECTION .data]
extern H
extern I
[SECTION .text]
[GLOBAL _start]
_start
jmp bootup
bootup:
mov ax, 0xb800
mov es,ax
mov al, BYTE [H]
mov byte[es:0x0f9c] ,al
mov al, BYTE [I]
mov byte[es:0x0f9e], al
pause:
jmp pause
ret
times 510-($-$$) db 0
dw 0xAA55
After I have created the two files above, I first compile the hello.c file with the following command:
cc -c hello.c
this command creates the file 'hello.o' in the current directory. Next I assemble the hello. s file with
the following command:
nasm -f aout -o hello_asm.o hello.s
This command creates the file 'hello_asm.o' in the current directory. Next I link the hello.o and
hello_asm.o into a file called hello.bin with the following command:
ld -o hello.bin -oformat binary -Ttext=0x00 hello.o hello_asm.o
This command creates the file 'hello.bin' into the current directory. Lastly, I copy the hello.bin binary
to the boot sector of a disk with the command:
dd if=hello.bin of=/dev/fd0 bs=512 count=1
When i reboot my system with the diskette in the disk drive I only get a blank screen. Can anyone
please help me with this problem.
Thanks
Michael Morrison