LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2004, 07:36 PM   #1
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
gcc/inlineasm question


Two question
1) Why doesn't this draw a diagnoal line? Is counter's register being curropted
2) For clobber section, do I need to define ax/eax if using ah/al?

gcc -c file.cpp -fwritable-strings -ffreestanding -nostartfiles -fno-rtti -fno-exceptions
ld --oformat binary -o line file.o
Code:
#define init12() \
	do {     \
	   asm volatile("mov $0x12,%%al\n \
	                 int $0x10\n" ::: \
			      "%al","%ax","%eax","%ah");} \
	while(0)
#define init13() \
	do {     \
	   asm volatile("mov $0x13,%%al\n \
	                 int $0x10\n" ::: \
			      "%al","%ax","%eax","%ah");} \
	while(0)
#define init14() \
	do {     \
	   asm volatile("mov $0x14,%%al\n \
	                 int $0x10\n" ::: \
			      "%al","%ax","%eax","%ah");} \
	while(0)
#define plotpix(x,y) \
        do {         \
	   asm volatile("mov $0x0C,%%ah\n   \
	                 mov $15,%%al\n     \
			 mov %0,%%ecx\n     \
			 mov %1,%%edx\n     \
			 int $0x10\n"    \
			 ::"r"(x),"r"(y) : \
				   "%ah","%ax","%eax","%ecx","%al", "%edx");} \
	while(0)


char *buffer = "Karlan Kayne\0";
int main(){
init12();

for(int counter = 0 ; counter < 30 ; counter++){
	plotpix(counter, counter);
	}

for(;;){}
return 0;
}
And the bootloader i'm using, if that helps:

nasm -f bin bootloader.S
cat bootloader line | dd of=/dev/fd0 bs=512
Code:
[BITS 16]
[ORG 0x7C00]   ; Origin location

_start:
     mov ah, 0x02    ; Read Disk Sectors
     mov al, 0x03    ; Read three sector only (512 bytes per sector)
     mov ch, 0x00    ; Track 0
     mov cl, 0x02    ; Sector 2
     mov dh, 0x00    ; Head 0
     mov dl, 0x00    ; Drive 0 (Floppy 1) (This can be replaced with the value in BootDrv)
     mov bx, 0x2000  ; Segment 0x2000
     mov es, bx      ;  again remember segments bust be loaded from non immediate data
     mov bx, 0x0000  ; Start of segment - offset value
.readsector:
     int 0x13        ; Call BIOS Read Disk Sectors function
     jc .readsector  ; If there was an error, try again
     mov ax, 0x2000  ; Set the data segment register
     mov ds, ax      ;  to point to the kernel location in memory
     jmp 0x2000:0x0000       ; Jump to the kernel

times 510-($-$$) db 0	; Fill the rest with zeros
dw 0xAA55		; Boot loader signature
And finally my c program in asm
Code:
	.file	"file.cpp"
.globl buffer
	.data
.LC0:
	.string	"Karlan Kayne"
	.string	""
	.align 4
	.type	buffer,@object
	.size	buffer,4
buffer:
	.long	.LC0
	.text
	.align 2
.globl main
	.type	main,@function
main:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%esi
	pushl	%ebx
	subl	$16, %esp
	andl	$-16, %esp
	movl	$0, %eax
	subl	%eax, %esp
#APP
	mov $0x12,%al
 	                 int $0x10

#NO_APP
	movl	$0, -12(%ebp)
.L3:
	cmpl	$29, -12(%ebp)
	jle	.L6
	jmp	.L4
.L6:
	movl	-12(%ebp), %ebx
	movl	-12(%ebp), %esi
#APP
	mov $0x0C,%ah
   	                 mov $15,%al
     			 mov %ebx,%ecx
     			 mov %esi,%edx
     			 int $0x10

#NO_APP
	leal	-12(%ebp), %eax
	incl	(%eax)
	jmp	.L3
.L4:
.L8:
	jmp	.L8
.Lfe1:
	.size	main,.Lfe1-main
	.ident	"GCC: (GNU) 3.2.3"
If anyone wonders what I'm doing: It will be a couple of arcade games(snake, pong, etc) all on a independent boot disc.

Last edited by karlan; 07-14-2004 at 07:46 PM.
 
Old 07-15-2004, 05:03 AM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
I see a problem...
The computer boots up in real mode (16 bit), but your program is
32bit. The CPU cant properly decode the instructions while it is in 16 bit
mode if the instructions it is given are encoded in 32bits.
Im guessing everything goes well until your bootloader puts the
kernel image into memory.

You have a few choices:

Write code in your bootloader that enters protected mode, then load your
kernel. Then you can use C 99% of the time. I think this would be overkill
for what you are doing though.

Write 16 bit real mode code, although you will have to write your code in
assembly. You can only access 1MB of memory, although that should be
enough.

Also, dont forget to set up a stack! Whether the stack pointer is valid or not
doest matter, it still needs to be at a known address in order to prevent
data overwrite.
 
Old 07-15-2004, 05:04 AM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
PS,

this forum may be more suited for these type of questions,
visit us sometimes...
http://www.mega-tokyo.com/forum/index.php?board=1
 
Old 07-15-2004, 07:07 PM   #4
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Original Poster
Rep: Reputation: 30
1) can I still use inturprets in protected?(EDIT: HOW DO I USE VM86 mode?)
2) how do i set up a stack
3) whats the asm code for entering protected mode(probley can find it somewhere else too, so no worries)

Last edited by karlan; 07-15-2004 at 08:05 PM.
 
Old 07-15-2004, 09:27 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
This page has alot of info:
http://mega-tokyo.com/osfaq2/

1) Yes, if you couldnt use interupts then the computer would be useless and I dont know
how to use VM86 mode yet, sorry. There is information at the above address though.

2) Easy, since the the stack is addressed using ss:sp you need to set them to an appropriate address. Keep in mind the stack grows DOWN so dont set it too close
to other data. Here's an example.

movw $0x9000,%ax // set ax to 0x9000
movw %ax,%ss //set ss equal to ax (0x9000)
movw $0x8000,%sp // set sp to 0x8000, now the stack is at 9000:8000

You can set the stack to any address you wish, its your responsibility to make sure
data isnt overwritten by pushes and pops.


3) This page has info on protected mode, http://www.mega-tokyo.com/osfaq2/ind.../ProtectedMode
If you are going to do any kind of protected mode programming I suggest
downloading the intel programmers manual from intel's website.
 
Old 07-15-2004, 11:01 PM   #6
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Rep: Reputation: 30
jinksys, that www.mega-tokyo.com url gives a 403 :-/
 
Old 07-16-2004, 11:54 AM   #7
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Odd, works for me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
asm Question Whiteghost Programming 2 09-03-2005 09:26 AM
Linux 32 ASM introuble Programming 2 05-10-2005 06:56 AM
ASM question zWaR Programming 2 06-26-2004 11:42 AM
question on usr/include/asm/module.h kmack2001 Linux - Newbie 0 02-14-2004 10:40 PM
Simple ASM question Citizen Bleys Programming 2 04-03-2003 05:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration