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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-14-2004, 07:36 PM
|
#1
|
Member
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158
Rep:
|
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.
|
|
|
07-15-2004, 05:03 AM
|
#2
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
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.
|
|
|
07-15-2004, 07:07 PM
|
#4
|
Member
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158
Original Poster
Rep:
|
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.
|
|
|
07-15-2004, 09:27 PM
|
#5
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
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.
|
|
|
07-15-2004, 11:01 PM
|
#6
|
Member
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196
Rep:
|
jinksys, that www.mega-tokyo.com url gives a 403 :-/
|
|
|
07-16-2004, 11:54 AM
|
#7
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
Odd, works for me.
|
|
|
All times are GMT -5. The time now is 03:15 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|