C + Asm combination for an OS [x86]
Posted: Sun Jul 22, 2007 6:08 pm
I am attempting to create my OS with a combination of assembler and c, i have a good stand alone assembler bootloader but when i use this tutorial's method of combining, my text no longer displays...
The Assembly
this merely displays a few lines of text on the screen, works well stand-alone
The C Code:
This code should be self-explanatory
The Batch File:
The batch file deletes old files, then it compiles the assembly with NASM using the aout format [even though it is written to .bin] then it compiles the .c files in my asm directory then it links them with ld using this linker script
The Linker Script:
The Linker file, not much to explain
I know this is a LONG post, sorry, but this allows you to now my circumstances. I would like to know why it displays jibberish on the screen as opposed to any message? Stand-alone the asm works fine, but when they are combined it does not function even though it never uses the c code
The Assembly
Code: Select all
[BITS 16]
[global main]
; Main program
main: ; Label for the start of the main program
mov ax,0x0000 ; Setup the Data Segment register
; Location of data is DS:Offset
mov ds,ax ; This can not be loaded directly it has to be in two steps.
; 'mov ds, 0x0000' will NOT work due to limitations on the CPU
mov si, Intro
call PutStr
call PutNewLine
mov si, HelloWorld ; Load the string into position for the procedure.
call PutStr ; Call/start the procedure
call PutNewLine
call PutNewLine
call PutNewLine
call PutNewLine
call PutNewLine
mov si, Conclusion
call PutStr
jmp $ ; Never ending loop
; Procedures
PutStr: ; Procedure label/start
; Set up the registers for the interrupt call
mov ah,0x0E ; The function to display a chacter (teletype)
mov bh,0x00 ; Page number
mov bl,0x07 ; Normal text attribute
.nextchar ; Internal label (needed to loop round for the next character)
lodsb ; I think of this as LOaD String Block
; (Not sure if thats the real meaning though)
; Loads [SI] into AL and increases SI by one
; Check for end of string '0'
or al,al ; Sets the zero flag if al = 0
; (OR outputs 0's where there is a zero bit in the register)
jz .return ; If the zero flag has been set go to the end of the procedure.
; Zero flag gets set when an instruction returns 0 as the answer.
int 0x10 ; Run the BIOS video interrupt
jmp .nextchar ; Loop back round to the top
.return ; Label at the end to jump to when complete
ret ; Return to main program
PutNewLine:
mov si, newLine
call PutStr
ret
;ExitSequence:
;inc cx
;cmp cx, 750
;je Quit
;jmp ExitSequence
; Data
Intro db ' OSTester: An Operating System Test',13,10,0
newLine db 13,10,0
HelloWorld db ' The Current Message Is As Follows: "Hello, World!"',13,10,0
Conclusion db ' Turn off the computer and remove the floppy disk to return to the normal OS',0
;Quit:
;do nothing
times 512-($-$$)-2 db 0
dw 0xAA55
The C Code:
Code: Select all
int main()
{
return 0;
}
int k_main()
{
int num;
char ch;
char *text_video = (char*)0xB8000;
char attrib = 0x07;
char *str="Kernel Loaded";
while(*str!=0)
{
*text_video = *str;
*text_video++;
*text_video = attrib;
*text_video++;
*str++;
}
return;
}
void clear_screen(char clear_to, char attrib)
{
char *text_video = (char*)0xB8000;
char *str="Kernel Loaded";
int pos=0;
while(pos<(80*25*2))
{
*text_video = clear_to;
*text_video++;
*text_video = attrib;
*str++;
pos++;
}
}
The Batch File:
Code: Select all
@echo off
del "C:\asm\ostest.bin"
del "C:\asm\ostest.img"
del "C:\asm\kernel.o"
echo Compiling assembly
echo nasm
C:\asm\nasm.exe -E C:\asm\errors.txt C:\asm\*.asm -f aout -o C:\asm\ostest.bin
echo gcc
C:\djgpp\bin\gcc.exe C:\asm\*.c
echo ld
C:\djgpp\bin\ld.exe C:\asm\link.ld -o C:\asm\kernel.o C:\asm\ostest.bin
echo Compile Attempt Finished
if exist "C:\asm\ostest.bin" (GOTO DoPartCopy)
GOTO exita
:DoPartCopy
copy /b C:\asm\ostest.bin+C:\asm\kernel.o C:\asm\ostest.img
call :InsertImgOnDisk "C:\asm\ostest.img"
GOTO exita
:InsertImgOnDisk
echo C:\asm\PartCopy.exe %1 0 %~z1 -f0
C:\asm\PartCopy.exe %1 0 %~z1 -f0
GOTO exitc
:exitb
del "C:\asm\kernel.o"
del "C:\asm\ostest.bin"
del "C:\asm\ostest.img"
echo .
echo Installation Attempt Failed
pause
:exita
del "C:\asm\kernel.o"
del "C:\asm\ostest.bin"
del "C:\asm\errors.txt"
del "C:\asm\ostest.img"
echo .
echo Installation Attempt Succeded
:exitc
The Linker Script:
Code: Select all
OUTPUT_FORMAT("binary")
ENTRY(main)
SECTIONS
{
.text 0xFF800000 : {
*(.text)
}
.data : {
*(.data)
}
.bss :
{
*(.bss)
}
}
I know this is a LONG post, sorry, but this allows you to now my circumstances. I would like to know why it displays jibberish on the screen as opposed to any message? Stand-alone the asm works fine, but when they are combined it does not function even though it never uses the c code