From: zak100 <zul...@ya...> - 2009-07-09 06:37:02
|
Hi, I am trying to call 'C" functions from my tiny kernel code. Can somebody plz help me with this? ; call c function extern [_K_main] ;--- test kernel.bin--- ; push cs ; push cs ; pop ds ; pop es mov ax, 7E0H mov ds,ax mov es,ax mov si, msg mov ah, 0Eh mov bx, 7 top: lodsb cmp al, 0 jz blackhole int 10h jmp short top blackhole: mov cx,1000 Delay: Loop Delay call _K_main; this is in main.c hlt jmp blackhole msg db 'Welcome to the Kernel!', 0 times 0x200-($-$$) db 0 I am getting following error mesg: D:\nasm prog>nasm -f bin -o boot.bin test_kernel2.asm test_kernel2.asm:5: error: label or instruction expected at start of line D:\NASMPR~1> Zulfi. -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24404865.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: H. P. A. <hp...@zy...> - 2009-07-09 15:13:49
|
zak100 wrote: > Hi, > I am trying to call 'C" functions from my tiny kernel code. Can somebody plz > help me with this? > ; call c function > extern [_K_main] That should be extern _K_main No brackets... -hpa -- H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf. |
From: Frank K. <fbk...@zy...> - 2009-07-09 15:52:33
|
H. Peter Anvin wrote: > zak100 wrote: >> Hi, >> I am trying to call 'C" functions from my tiny kernel code. Can somebody plz >> help me with this? >> ; call c function >> extern [_K_main] > > That should be > > extern _K_main > > No brackets... Now why haven't I seen my reply to Zulfi... in which I point out that "-f bin" isn't going to like it without the brackets, either? Maybe "nasm-users" doesn't like me... Anyway... I tried the following experiment... Since I don't "do" C (much), I made up a "fake" kmain, which simply prints the letter "K" on the screen and returns. Assembled with "-f obj". Added a "..start:" label to "test_kernel2", and assembled that with "-f obj". Since I've got Alink working under Linux, I tried to link 'em with "alink -oCOM test_kernel2.obj kmain.obj". Alink warns that it's discarding data at offset... well, all of 'em... and produces a zero-length .com file. That's not going to help. So tried "-oEXE". Got an .exe! Since I don't have an MZ loader at hand, nor "exe2bin" (which might work), I simply removed the header with a text editor. The offsets happened to work out - I doubt if this will always be true! I think we're going to need a proper MZ loader to handle relocations, eventually. But for now... I combined "boot.bin" with this result using "cat", and wrote the resulting "image.bin" to floppy. Wrote that to floppy with "dd". Booted, and after "Welcome to the kernel", saw 'K' printed... repeatedly. Progress! This isn't exactly what you want to do, but leads me to believe that, with the proper tools, it may not be as difficult as I imagined. (famous last words...) Best, Frank |
From: Frank K. <fbk...@zy...> - 2009-07-09 18:11:24
|
Tyler Littlefield wrote: > writing a kernel isn't to dificult; check out osdev.org if your in to that. > the issues start when you start to write a memory manager, etc etc. And then wrote: > ld will allow you to manage where your entry point is. > If your using grub, you can make your files elf which makes things a bit > easier for loading. > My current os dev environment uses linux and bochs for testing. I > compile the code, if successfull it generates my iso and I just cd ~ and > load up bochs for testing. > HTH somewhat, Hi Tyler, I don't mention it often enough: to reply to this list, you've gotta hit "reply all" - just "reply" sends only to the poster. Sorry that bit ya. I ASSume you wanted to reply to the list... if not, sorry again. :) I've been corresponding with Zulfi, and there have been messages on the "beginner's questions" forum (and elsewhere? nabble?), so to try to get everybody on the same page... What Zulfi has in mind is something like this: http://portal.acm.org/citation.cfm?id=1509022 ------------------------ ABSTRACT This paper describes a semester project where students design an operating system from the ground-up, capable of booting from a floppy disk on an actual machine. Unlike previous projects of this kind, this project was designed for students with only one semester of programming experience and no prior exposure to data structures, assembly language, or computer organization. Students nevertheless wrote a full system consisting of system calls, program execution, a file system, a command-line shell, and support for multiprocessing. The project was assigned to a class and successfully completed by nearly every student. ------------------------- Sounds wildly optimistic to me, but... The idea is to "hand" the students a bootsector, and another few routines in asm, and they write the C parts (after one year of Java, in Black's experiment!). I've been trying to help out with the asm parts (I guess that's going okay), but I'm clueless in C (only use for it is printing floats with printf... and that's not gonna work here! :) Zulfi's on a Windows platform, and I (and Black, and you, and most everybody "serious") run Linux. This doesn't help with the "tools" issue (although a lot of it has been ported). I don't know if this is "negotiable". This is going to be a 16-bit OS (be thankful for small favors). I suspect your suggestion to check out osdev.org and/or alt.os.development is probably the next stop. Best, Frank |
From: zak100 <zul...@ya...> - 2009-07-10 05:07:14
|
Hi Peter and Frank, I am able to compile the code after removing the brackets but now I am getting prob with ld. D:\nasm prog>nasm -f elf test_kernel2.asm -o test.o D:\NASMPR~1>ld -o kernel.exe test.o main1.o test.o:(.text+0xa): relocation truncated to fit: R_386_16 against `.text' D:\NASMPR~1>dir k* Volume in drive D is New Volume Volume Serial Number is 70FD-1D2C Directory of D:\NASMPR~1 File Not Found D:\NASMPR~1> I downloaded alib but some files were missing. Thanks for your attention but I am still looking for more help on this using ld. In the meantime, i would be looking for alib again. Zulfi. Frank Kotler-3 wrote: > > H. Peter Anvin wrote: >> zak100 wrote: >>> Hi, >>> I am trying to call 'C" functions from my tiny kernel code. Can somebody >>> plz >>> help me with this? >>> ; call c function >>> extern [_K_main] >> >> That should be >> >> extern _K_main >> >> No brackets... > > Now why haven't I seen my reply to Zulfi... in which I point out that > "-f bin" isn't going to like it without the brackets, either? Maybe > "nasm-users" doesn't like me... > > Anyway... I tried the following experiment... Since I don't "do" C > (much), I made up a "fake" kmain, which simply prints the letter "K" on > the screen and returns. Assembled with "-f obj". Added a "..start:" > label to "test_kernel2", and assembled that with "-f obj". Since I've > got Alink working under Linux, I tried to link 'em with "alink -oCOM > test_kernel2.obj kmain.obj". Alink warns that it's discarding data at > offset... well, all of 'em... and produces a zero-length .com file. > That's not going to help. So tried "-oEXE". Got an .exe! Since I don't > have an MZ loader at hand, nor "exe2bin" (which might work), I simply > removed the header with a text editor. The offsets happened to work out > - I doubt if this will always be true! I think we're going to need a > proper MZ loader to handle relocations, eventually. But for now... > > I combined "boot.bin" with this result using "cat", and wrote the > resulting "image.bin" to floppy. Wrote that to floppy with "dd". Booted, > and after "Welcome to the kernel", saw 'K' printed... repeatedly. > Progress! > > This isn't exactly what you want to do, but leads me to believe that, > with the proper tools, it may not be as difficult as I imagined. (famous > last words...) > > Best, > Frank > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full > prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Nasm-users mailing list > Nas...@li... > https://lists.sourceforge.net/lists/listinfo/nasm-users > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24421875.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: Frank K. <fbk...@zy...> - 2009-07-10 13:33:36
|
zak100 wrote: > Hi, > I am able to make the binary using alink. partcopy was giving prob but it > was handled as done previously. > > The steps are provided below: > D:\nasm prog>nasm -f bin -o boot.bin BLKernelT.asm > > D:\NASMPR~1>nasm -f bin -o writeit.com writeit.asm > > D:\NASMPR~1>writeit > > D:\NASMPR~1>del test_kernel2.onj > Could Not Find D:\NASMPR~1\test_kernel2.onj > > D:\NASMPR~1>del test_kernel2.obj > > D:\NASMPR~1>nasm -f obj test_kernel2.asm > > D:\NASMPR~1>dir t*.obj > Volume in drive D is New Volume > Volume Serial Number is 70FD-1D2C > > Directory of D:\NASMPR~1 > > 07/10/2009 10:55 AM 642 test_kernel2.obj > 1 File(s) 642 bytes > 0 Dir(s) 22,656,360,448 bytes free > > D:\NASMPR~1>alink -o kernel.bin test_kernel2.obj main1.o > ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. > All Rights Reserved > > Loading file test_kernel2.obj > Loading file main1.o > matched Externs > matched ComDefs > Warning, no entry point specified You can fix this by adding a "..start:" label where you want your entrypoint. > Warning - no stack segment stack stack resb ???? ; how big do you want? C uses a lot of stack, I understand. The first "stack" is just a name - could be anything. The second "stack" is necessary to let Nasm know what we want. I've been ignoring the warning, and "let the OS take care of the stack". We set up a stack in the bootsector which should be "okay for now". When you get to task switching/multitasking, you'll want to swap stacks for each process (I imagine). > D:\NASMPR~1>alink -o kernel.exe test_kernel2.obj main1.o > ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. > All Rights Reserved > > Loading file test_kernel2.obj > Loading file main1.o > matched Externs > matched ComDefs > Warning, no entry point specified > Warning - no stack > > D:\NASMPR~1>del kernel.bin > > D:\NASMPR~1>exe2bin kernel.exe kernel.bin Good. This produces a .com file (no matter what we call it) which expects to be loaded at 100h into... whatever segment. > D:\NASMPR~1>partcopy kernel.bin 0 200 -f0 200 > Failed to read source at offset 144 > D:\NASMPR~1>partcopy kernel.bin 0 144 -f0 200 > > D:\NASMPR~1> > ----------------------------------- > > But when I rebooted , I got boot sector statment but I am not seeing the > "welcome....." kernel message. > Can somebody plz guide me?? Try jumping to 7D0h:100h (assuming that you're writing this to 7E00h). I haven't done anything further with this. I've downloaded source (in assembly) for an exe2bin clone, thinking we might be able to modify it to produce a binary at "org 0", or perhaps create a loader for MZ to be included in our code. Only just begun to look at it. Tyler mentioned using bochs for testing. That would speed up development a *lot* (especially for me, since I don't like rebooting). I've downloaded bochs a couple of times, but never tried it. I've seen too many posts about "my bootsector works in bochs, but not in a real machine" (or vise versa), and "bochs is giving me this error message, what the heck does it mean?"... suggesting that bochs doesn't accurately reflect "real hardware" (can't match *all* of 'em, of course), and has a "learning curve" to it... Any tips, Tyler? Is bochs going to insist on seeing a full 1.44M image before it'll boot from a "fake floppy"? Well, I'll figure it out (or not), if and when I get around to trying it... Best, Frank |
From: zak100 <zul...@ya...> - 2009-07-10 18:05:17
|
Hi, The changes for jump code lies here: GiveControlToOS: ; PROC LEA AX,[szDone] Call PrintMessage CALL GetKey ; db 0e9h ;Far JMP op code. ; dw 512 ;JMP 512 bytes ahead ;jmp 0:7E00h;-----------------------------------At this point we are going to Kernel code; for including C prog:test_kernel2.asm ;jmp 7D0h:100h jmp 0:7D00h ; POP AX ;//Another why to make ;the CPU jump to a new place. ; POP AX ; Push 7E0h ;Push New CS address. ; Push 0 ;Push New IP address. ;The address that comes out is 7E00:0000. ; comment does not match code! ;(512 bytes Higher from were BIOS Put us.) retf ; you'd want "retf" here. ;GiveControlToOS EndP The changes for ..start lie here ..start: extern _K_main ;--- test kernel.bin--- ; push cs ; push cs ; pop ds ; pop es mov ax, 7E0H mov ds,ax mov es,ax mov si, msg mov ah, 0Eh mov bx, 7 top: lodsb cmp al, 0 jz blackhole int 10h jmp short top blackhole: mov cx,1000 Delay: Loop Delay call _K_main; this is in main.c hlt jmp blackhole msg db 'Welcome to the Kernel!', 0 times 0x200-($-$$) db 0 Kindly help me with this. Zulfi. Frank Kotler-3 wrote: > > zak100 wrote: >> Hi, >> I am able to make the binary using alink. partcopy was giving prob but it >> was handled as done previously. >> >> The steps are provided below: >> D:\nasm prog>nasm -f bin -o boot.bin BLKernelT.asm >> >> D:\NASMPR~1>nasm -f bin -o writeit.com writeit.asm >> >> D:\NASMPR~1>writeit >> >> D:\NASMPR~1>del test_kernel2.onj >> Could Not Find D:\NASMPR~1\test_kernel2.onj >> >> D:\NASMPR~1>del test_kernel2.obj >> >> D:\NASMPR~1>nasm -f obj test_kernel2.asm >> >> D:\NASMPR~1>dir t*.obj >> Volume in drive D is New Volume >> Volume Serial Number is 70FD-1D2C >> >> Directory of D:\NASMPR~1 >> >> 07/10/2009 10:55 AM 642 test_kernel2.obj >> 1 File(s) 642 bytes >> 0 Dir(s) 22,656,360,448 bytes free >> >> D:\NASMPR~1>alink -o kernel.bin test_kernel2.obj main1.o >> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >> All Rights Reserved >> >> Loading file test_kernel2.obj >> Loading file main1.o >> matched Externs >> matched ComDefs >> Warning, no entry point specified > > You can fix this by adding a "..start:" label where you want your > entrypoint. > >> Warning - no stack > > segment stack stack > resb ???? ; how big do you want? C uses a lot of stack, I understand. > > The first "stack" is just a name - could be anything. The second "stack" > is necessary to let Nasm know what we want. I've been ignoring the > warning, and "let the OS take care of the stack". We set up a stack in > the bootsector which should be "okay for now". When you get to task > switching/multitasking, you'll want to swap stacks for each process (I > imagine). > >> D:\NASMPR~1>alink -o kernel.exe test_kernel2.obj main1.o >> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >> All Rights Reserved >> >> Loading file test_kernel2.obj >> Loading file main1.o >> matched Externs >> matched ComDefs >> Warning, no entry point specified >> Warning - no stack >> >> D:\NASMPR~1>del kernel.bin >> >> D:\NASMPR~1>exe2bin kernel.exe kernel.bin > > Good. This produces a .com file (no matter what we call it) which > expects to be loaded at 100h into... whatever segment. > >> D:\NASMPR~1>partcopy kernel.bin 0 200 -f0 200 >> Failed to read source at offset 144 >> D:\NASMPR~1>partcopy kernel.bin 0 144 -f0 200 >> >> D:\NASMPR~1> >> ----------------------------------- >> >> But when I rebooted , I got boot sector statment but I am not seeing the >> "welcome....." kernel message. >> Can somebody plz guide me?? > > Try jumping to 7D0h:100h (assuming that you're writing this to 7E00h). > > I haven't done anything further with this. I've downloaded source (in > assembly) for an exe2bin clone, thinking we might be able to modify it > to produce a binary at "org 0", or perhaps create a loader for MZ to be > included in our code. Only just begun to look at it. > > Tyler mentioned using bochs for testing. That would speed up development > a *lot* (especially for me, since I don't like rebooting). I've > downloaded bochs a couple of times, but never tried it. I've seen too > many posts about "my bootsector works in bochs, but not in a real > machine" (or vise versa), and "bochs is giving me this error message, > what the heck does it mean?"... suggesting that bochs doesn't accurately > reflect "real hardware" (can't match *all* of 'em, of course), and has a > "learning curve" to it... > > Any tips, Tyler? Is bochs going to insist on seeing a full 1.44M image > before it'll boot from a "fake floppy"? Well, I'll figure it out (or > not), if and when I get around to trying it... > > Best, > Frank > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full > prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Nasm-users mailing list > Nas...@li... > https://lists.sourceforge.net/lists/listinfo/nasm-users > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24432173.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: zak100 <zul...@ya...> - 2009-07-10 18:07:22
|
Hi, The changes for jump code lies here: GiveControlToOS: ; PROC LEA AX,[szDone] Call PrintMessage CALL GetKey ; db 0e9h ;Far JMP op code. ; dw 512 ;JMP 512 bytes ahead ;jmp 0:7E00h;-----------------------------------At this point we are going to Kernel code; for including C prog:test_kernel2.asm ;jmp 7D0h:100h jmp 0:7D00h ; POP AX ;//Another why to make ;the CPU jump to a new place. ; POP AX ; Push 7E0h ;Push New CS address. ; Push 0 ;Push New IP address. ;The address that comes out is 7E00:0000. ; comment does not match code! ;(512 bytes Higher from were BIOS Put us.) retf ; you'd want "retf" here. ;GiveControlToOS EndP The changes for ..start lie here ..start: extern _K_main ;--- test kernel.bin--- ; push cs ; push cs ; pop ds ; pop es mov ax, 7E0H mov ds,ax mov es,ax mov si, msg mov ah, 0Eh mov bx, 7 top: lodsb cmp al, 0 jz blackhole int 10h jmp short top blackhole: mov cx,1000 Delay: Loop Delay call _K_main; this is in main.c hlt jmp blackhole msg db 'Welcome to the Kernel!', 0 times 0x200-($-$$) db 0 Kindly help me with this. Zulfi. Frank Kotler-3 wrote: > > zak100 wrote: >> Hi, >> I am able to make the binary using alink. partcopy was giving prob but it >> was handled as done previously. >> >> The steps are provided below: >> D:\nasm prog>nasm -f bin -o boot.bin BLKernelT.asm >> >> D:\NASMPR~1>nasm -f bin -o writeit.com writeit.asm >> >> D:\NASMPR~1>writeit >> >> D:\NASMPR~1>del test_kernel2.onj >> Could Not Find D:\NASMPR~1\test_kernel2.onj >> >> D:\NASMPR~1>del test_kernel2.obj >> >> D:\NASMPR~1>nasm -f obj test_kernel2.asm >> >> D:\NASMPR~1>dir t*.obj >> Volume in drive D is New Volume >> Volume Serial Number is 70FD-1D2C >> >> Directory of D:\NASMPR~1 >> >> 07/10/2009 10:55 AM 642 test_kernel2.obj >> 1 File(s) 642 bytes >> 0 Dir(s) 22,656,360,448 bytes free >> >> D:\NASMPR~1>alink -o kernel.bin test_kernel2.obj main1.o >> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >> All Rights Reserved >> >> Loading file test_kernel2.obj >> Loading file main1.o >> matched Externs >> matched ComDefs >> Warning, no entry point specified > > You can fix this by adding a "..start:" label where you want your > entrypoint. > >> Warning - no stack > > segment stack stack > resb ???? ; how big do you want? C uses a lot of stack, I understand. > > The first "stack" is just a name - could be anything. The second "stack" > is necessary to let Nasm know what we want. I've been ignoring the > warning, and "let the OS take care of the stack". We set up a stack in > the bootsector which should be "okay for now". When you get to task > switching/multitasking, you'll want to swap stacks for each process (I > imagine). > >> D:\NASMPR~1>alink -o kernel.exe test_kernel2.obj main1.o >> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >> All Rights Reserved >> >> Loading file test_kernel2.obj >> Loading file main1.o >> matched Externs >> matched ComDefs >> Warning, no entry point specified >> Warning - no stack >> >> D:\NASMPR~1>del kernel.bin >> >> D:\NASMPR~1>exe2bin kernel.exe kernel.bin > > Good. This produces a .com file (no matter what we call it) which > expects to be loaded at 100h into... whatever segment. > >> D:\NASMPR~1>partcopy kernel.bin 0 200 -f0 200 >> Failed to read source at offset 144 >> D:\NASMPR~1>partcopy kernel.bin 0 144 -f0 200 >> >> D:\NASMPR~1> >> ----------------------------------- >> >> But when I rebooted , I got boot sector statment but I am not seeing the >> "welcome....." kernel message. >> Can somebody plz guide me?? > > Try jumping to 7D0h:100h (assuming that you're writing this to 7E00h). > > I haven't done anything further with this. I've downloaded source (in > assembly) for an exe2bin clone, thinking we might be able to modify it > to produce a binary at "org 0", or perhaps create a loader for MZ to be > included in our code. Only just begun to look at it. > > Tyler mentioned using bochs for testing. That would speed up development > a *lot* (especially for me, since I don't like rebooting). I've > downloaded bochs a couple of times, but never tried it. I've seen too > many posts about "my bootsector works in bochs, but not in a real > machine" (or vise versa), and "bochs is giving me this error message, > what the heck does it mean?"... suggesting that bochs doesn't accurately > reflect "real hardware" (can't match *all* of 'em, of course), and has a > "learning curve" to it... > > Any tips, Tyler? Is bochs going to insist on seeing a full 1.44M image > before it'll boot from a "fake floppy"? Well, I'll figure it out (or > not), if and when I get around to trying it... > > Best, > Frank > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full > prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Nasm-users mailing list > Nas...@li... > https://lists.sourceforge.net/lists/listinfo/nasm-users > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24432204.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: Frank K. <fbk...@zy...> - 2009-07-09 15:54:57
|
zak100 wrote: > Hi, > I am trying to call 'C" functions from my tiny kernel code. Can somebody plz > help me with this? > ; call c function > extern [_K_main] > ;--- test kernel.bin--- > > ; push cs > ; push cs > ; pop ds > ; pop es > mov ax, 7E0H > mov ds,ax > mov es,ax > mov si, msg > > mov ah, 0Eh > mov bx, 7 > top: > lodsb > cmp al, 0 > jz blackhole > int 10h > jmp short top > > blackhole: > > mov cx,1000 > Delay: Loop Delay > call _K_main; this is in main.c > hlt > jmp blackhole > > msg db 'Welcome to the Kernel!', 0 > > times 0x200-($-$$) db 0 > > > I am getting following error mesg: > D:\nasm prog>nasm -f bin -o boot.bin test_kernel2.asm > test_kernel2.asm:5: error: label or instruction expected at start of line Hi Zulfi, Removing the brackets will at least give you correct syntax (extern _K_main), but binary format still doesn't know about external references. What format is your proposed _K_main in? OMF probably, for 16-bit code(???). Try assembling with "nasm -f obj test_kernel2.asm" this will produce "test_kernel2.obj", by default. If your linker won't handle long file names, you can shorten it with the "-o" switch, but your linker is probably going to be happier with ".obj" than ".bin" (maybe not?). Then... your linker is probably going to want to produce an MZ executable by default. I don't think it's too hard to write a loader for MZ, but it might be easier to persuade your linker to produce a .com file. I believe ld (for 32-bit Linux - won't help you) can be coerced to produce a flat binary, explicitly specifying origins for the various sections (if you know how - I don't). If your linker will do that, you're in luck. Failing that... I dunno. Make sure your compiler isn't producing any "dosisms" in your C code, of course. The OS-development guys may have better advice on this. Best, Frank P.S. Thanks for asking here, rather than nasm-devel, Zulfi! :) |
From: zak100 <zul...@ya...> - 2009-07-17 05:05:06
|
Hi, Thanks for your help. I have received your mz folder. It has two binaries: boot and image. 'boot' is fine but when i tried to load image to sector 2 it didnt work. Maybe I am doing something wrong. However, I didnt find any 'C' files. Still waiting for your response in this regard. Zulfi. Frank Kotler-3 wrote: > > zak100 wrote: >> Hi, >> I am trying to call 'C" functions from my tiny kernel code. Can somebody >> plz >> help me with this? >> ; call c function >> extern [_K_main] >> ;--- test kernel.bin--- >> >> ; push cs >> ; push cs >> ; pop ds >> ; pop es >> mov ax, 7E0H >> mov ds,ax >> mov es,ax >> mov si, msg >> >> mov ah, 0Eh >> mov bx, 7 >> top: >> lodsb >> cmp al, 0 >> jz blackhole >> int 10h >> jmp short top >> >> blackhole: >> >> mov cx,1000 >> Delay: Loop Delay >> call _K_main; this is in main.c >> hlt >> jmp blackhole >> >> msg db 'Welcome to the Kernel!', 0 >> >> times 0x200-($-$$) db 0 >> >> >> I am getting following error mesg: >> D:\nasm prog>nasm -f bin -o boot.bin test_kernel2.asm >> test_kernel2.asm:5: error: label or instruction expected at start of line > > Hi Zulfi, > > Removing the brackets will at least give you correct syntax (extern > _K_main), but binary format still doesn't know about external > references. What format is your proposed _K_main in? OMF probably, for > 16-bit code(???). > > Try assembling with "nasm -f obj test_kernel2.asm" this will produce > "test_kernel2.obj", by default. If your linker won't handle long file > names, you can shorten it with the "-o" switch, but your linker is > probably going to be happier with ".obj" than ".bin" (maybe not?). > > Then... your linker is probably going to want to produce an MZ > executable by default. I don't think it's too hard to write a loader for > MZ, but it might be easier to persuade your linker to produce a .com > file. I believe ld (for 32-bit Linux - won't help you) can be coerced to > produce a flat binary, explicitly specifying origins for the various > sections (if you know how - I don't). If your linker will do that, > you're in luck. Failing that... I dunno. > > Make sure your compiler isn't producing any "dosisms" in your C code, of > course. > > The OS-development guys may have better advice on this. > > Best, > Frank > > P.S. Thanks for asking here, rather than nasm-devel, Zulfi! :) > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full > prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Nasm-users mailing list > Nas...@li... > https://lists.sourceforge.net/lists/listinfo/nasm-users > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24528638.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: Frank K. <fbk...@zy...> - 2009-07-17 10:23:26
|
zak100 wrote: > Hi, > Thanks for your help. > > > I have received your mz folder. It has two binaries: boot and image. 'boot' > is fine > but when i tried to load image to sector 2 it didnt work. Maybe I am doing > something > wrong. What *I* do is to combine "boot.bin" plus "mzt1.exe"(!) into "image.bin", and load the whole thing, starting at sector 1. You could use "image.bin", loading it to sector 1 - "writeit" won't do (only does 512 bytes), but "partcopy" ought to - "partcopy image.bin 0 323 -f0", I think would do it. (I create "image.bin" with "cat", but you could use "copy /b boot.bin+mzt1.exe image bin", I think) Alternatively, you could write them separately - "boot.bin" with "writeit" or "partcopy", and "partcopy" for "mzt1.exe". The idea is simply to eliminate the "exe2bin" step, and load an MZ executable. > However, I didnt find any 'C' files. Heh! No, I'm not taking the course, so I don't have to follow the rules. :) I don't "do" C, unless absolutely necessary - haven't got a 16-bit compiler, in any case. If you look at "kmain.asm", "write.asm" and "getkey.asm", you'll see that they're "fake C" - you "should" be able to replace them with "real C" equivalents... if I've done it right (big if). "mzt1.asm" serves the purpose (approximately) of "crt0" or whatever your compiler calls the "startup code" that calls "main" - "kmain", in this case. I've modified your bootsector (the one we "translated" from Tasmese) - among other things, I altered it to read just one sector (reading 5 tracks wasn't working for me, for some reason). My "mzt1.exe" is well under that, so far, but when it gets more extensive, the bootsector is going to have to change. I mention this because, in my experience, C tends to produce "bloated" code, compared to asm, and if you go over 512 bytes, it's going to mysteriously quit working. Another way I haven't "followed the assignment" is that I used int 10h for "write()". I'm confident I can write a string to B800:????, if I can find the string! The purpose of this step of the exercise, for me, is to be able to find my data! I figure if I can find the string, I can find "cursor_x" and "cursor_y", and I know how to find the "screen". This seems to "work"... as long as I put code and data in the same segment. So far, my attempts to declare a "proper stack" and put "data" in "segment data" have failed. What I'm seeing in the executable header doesn't match what I "expect" to see. If and when I figure out how to deal with that, and with far calls and far data, I won't be stuck in a single 64k segment! (plus, it's not nice to teach beginners to ignore the warning about "no stack"! :) I don't know what compiler you're using, or what you're telling it. Are you sticking to "model tiny"? I'm not sure what a compiler (and Alink) will emit, from the code you posted. If you want to send me some "output" - either before or after "exe2bin", or both, I'll try to figure out how we might load it (if you're still having trouble with that). No guarantee - I can't always figure out how to load my own stuff. :) "Post exe2bin" should be "easier"... maybe... Sorry I wasn't clearer about how to use that example. I was pretty tired when I posted. ("try this *one* more thing and test it and then I'm going to bed!"...) I should have mentioned that while "build" won't work in dos (a bash script), it has some clues to how to put the thing together. Lemme see if I remember how to write a dos batch file... call nasm -f bin bootmz.asm -o boot.bin call nasm -f obj mzt1.asm call nasm -f obj kmain.asm call nasm -f obj write.asm call nasm -f obj getkey.asm call alink -oEXE mzt1.obj kmain.obj write.obj getkey.obj call copy /b boot.bin+mzt1.exe image.bin call partcopy image.bin 0 323 -f0 echo done That's totally untested (and doesn't check returned errorlevels). Good luck! Best, Frank |
From: H. P. A. <hp...@zy...> - 2009-07-18 06:01:31
|
Frank Kotler wrote: > I don't "do" C, unless absolutely necessary - haven't got a 16-bit > compiler, in any case. If you need one, OpenWatcom is quite good and free. -hpa |
From: zak100 <zul...@ya...> - 2009-07-18 13:49:00
|
Its working now. Thanks for your time, commands, description and for your effort. Zulfi. Frank Kotler-3 wrote: > > zak100 wrote: >> Hi, >> Thanks for your help. >> >> >> I have received your mz folder. It has two binaries: boot and image. >> 'boot' >> is fine >> but when i tried to load image to sector 2 it didnt work. Maybe I am >> doing >> something >> wrong. > > What *I* do is to combine "boot.bin" plus "mzt1.exe"(!) into > "image.bin", and load the whole thing, starting at sector 1. You could > use "image.bin", loading it to sector 1 - "writeit" won't do (only does > 512 bytes), but "partcopy" ought to - "partcopy image.bin 0 323 -f0", I > think would do it. (I create "image.bin" with "cat", but you could use > "copy /b boot.bin+mzt1.exe image bin", I think) > > Alternatively, you could write them separately - "boot.bin" with > "writeit" or "partcopy", and "partcopy" for "mzt1.exe". The idea is > simply to eliminate the "exe2bin" step, and load an MZ executable. > >> However, I didnt find any 'C' files. > > Heh! No, I'm not taking the course, so I don't have to follow the rules. > :) > > I don't "do" C, unless absolutely necessary - haven't got a 16-bit > compiler, in any case. If you look at "kmain.asm", "write.asm" and > "getkey.asm", you'll see that they're "fake C" - you "should" be able to > replace them with "real C" equivalents... if I've done it right (big > if). "mzt1.asm" serves the purpose (approximately) of "crt0" or whatever > your compiler calls the "startup code" that calls "main" - "kmain", in > this case. > > I've modified your bootsector (the one we "translated" from Tasmese) - > among other things, I altered it to read just one sector (reading 5 > tracks wasn't working for me, for some reason). My "mzt1.exe" is well > under that, so far, but when it gets more extensive, the bootsector is > going to have to change. I mention this because, in my experience, C > tends to produce "bloated" code, compared to asm, and if you go over 512 > bytes, it's going to mysteriously quit working. > > Another way I haven't "followed the assignment" is that I used int 10h > for "write()". I'm confident I can write a string to B800:????, if I can > find the string! The purpose of this step of the exercise, for me, is to > be able to find my data! I figure if I can find the string, I can find > "cursor_x" and "cursor_y", and I know how to find the "screen". This > seems to "work"... as long as I put code and data in the same segment. > So far, my attempts to declare a "proper stack" and put "data" in > "segment data" have failed. What I'm seeing in the executable header > doesn't match what I "expect" to see. If and when I figure out how to > deal with that, and with far calls and far data, I won't be stuck in a > single 64k segment! (plus, it's not nice to teach beginners to ignore > the warning about "no stack"! :) > > I don't know what compiler you're using, or what you're telling it. Are > you sticking to "model tiny"? I'm not sure what a compiler (and Alink) > will emit, from the code you posted. If you want to send me some > "output" - either before or after "exe2bin", or both, I'll try to figure > out how we might load it (if you're still having trouble with that). No > guarantee - I can't always figure out how to load my own stuff. :) "Post > exe2bin" should be "easier"... maybe... > > Sorry I wasn't clearer about how to use that example. I was pretty tired > when I posted. ("try this *one* more thing and test it and then I'm > going to bed!"...) I should have mentioned that while "build" won't work > in dos (a bash script), it has some clues to how to put the thing > together. > > Lemme see if I remember how to write a dos batch file... > > call nasm -f bin bootmz.asm -o boot.bin > > call nasm -f obj mzt1.asm > call nasm -f obj kmain.asm > call nasm -f obj write.asm > call nasm -f obj getkey.asm > > call alink -oEXE mzt1.obj kmain.obj write.obj getkey.obj > > call copy /b boot.bin+mzt1.exe image.bin > call partcopy image.bin 0 323 -f0 > echo done > > That's totally untested (and doesn't check returned errorlevels). Good > luck! > > Best, > Frank > > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full > prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Nasm-users mailing list > Nas...@li... > https://lists.sourceforge.net/lists/listinfo/nasm-users > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24547996.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: zak100 <zul...@ya...> - 2009-07-10 06:11:04
|
Hi, I am able to make the binary using alink. partcopy was giving prob but it was handled as done previously. The steps are provided below: D:\nasm prog>nasm -f bin -o boot.bin BLKernelT.asm D:\NASMPR~1>nasm -f bin -o writeit.com writeit.asm D:\NASMPR~1>writeit D:\NASMPR~1>del test_kernel2.onj Could Not Find D:\NASMPR~1\test_kernel2.onj D:\NASMPR~1>del test_kernel2.obj D:\NASMPR~1>nasm -f obj test_kernel2.asm D:\NASMPR~1>dir t*.obj Volume in drive D is New Volume Volume Serial Number is 70FD-1D2C Directory of D:\NASMPR~1 07/10/2009 10:55 AM 642 test_kernel2.obj 1 File(s) 642 bytes 0 Dir(s) 22,656,360,448 bytes free D:\NASMPR~1>alink -o kernel.bin test_kernel2.obj main1.o ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. All Rights Reserved Loading file test_kernel2.obj Loading file main1.o matched Externs matched ComDefs Warning, no entry point specified Warning - no stack D:\NASMPR~1>alink -o kernel.exe test_kernel2.obj main1.o ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. All Rights Reserved Loading file test_kernel2.obj Loading file main1.o matched Externs matched ComDefs Warning, no entry point specified Warning - no stack D:\NASMPR~1>del kernel.bin D:\NASMPR~1>exe2bin kernel.exe kernel.bin D:\NASMPR~1>partcopy kernel.bin 0 200 -f0 200 Failed to read source at offset 144 D:\NASMPR~1>partcopy kernel.bin 0 144 -f0 200 D:\NASMPR~1> ----------------------------------- But when I rebooted , I got boot sector statment but I am not seeing the "welcome....." kernel message. Can somebody plz guide me?? Zulfi. zak100 wrote: > > Hi Peter and Frank, > I am able to compile the code after removing the brackets but now I am > getting prob with ld. > D:\nasm prog>nasm -f elf test_kernel2.asm -o test.o > > D:\NASMPR~1>ld -o kernel.exe test.o main1.o > test.o:(.text+0xa): relocation truncated to fit: R_386_16 against `.text' > > D:\NASMPR~1>dir k* > Volume in drive D is New Volume > Volume Serial Number is 70FD-1D2C > > Directory of D:\NASMPR~1 > > File Not Found > > D:\NASMPR~1> > > I downloaded alib but some files were missing. Thanks for your attention > but I am still looking for more help on this using ld. > > In the meantime, i would be looking for alib again. > > Zulfi. > > > Frank Kotler-3 wrote: >> >> H. Peter Anvin wrote: >>> zak100 wrote: >>>> Hi, >>>> I am trying to call 'C" functions from my tiny kernel code. Can >>>> somebody plz >>>> help me with this? >>>> ; call c function >>>> extern [_K_main] >>> >>> That should be >>> >>> extern _K_main >>> >>> No brackets... >> >> Now why haven't I seen my reply to Zulfi... in which I point out that >> "-f bin" isn't going to like it without the brackets, either? Maybe >> "nasm-users" doesn't like me... >> >> Anyway... I tried the following experiment... Since I don't "do" C >> (much), I made up a "fake" kmain, which simply prints the letter "K" on >> the screen and returns. Assembled with "-f obj". Added a "..start:" >> label to "test_kernel2", and assembled that with "-f obj". Since I've >> got Alink working under Linux, I tried to link 'em with "alink -oCOM >> test_kernel2.obj kmain.obj". Alink warns that it's discarding data at >> offset... well, all of 'em... and produces a zero-length .com file. >> That's not going to help. So tried "-oEXE". Got an .exe! Since I don't >> have an MZ loader at hand, nor "exe2bin" (which might work), I simply >> removed the header with a text editor. The offsets happened to work out >> - I doubt if this will always be true! I think we're going to need a >> proper MZ loader to handle relocations, eventually. But for now... >> >> I combined "boot.bin" with this result using "cat", and wrote the >> resulting "image.bin" to floppy. Wrote that to floppy with "dd". Booted, >> and after "Welcome to the kernel", saw 'K' printed... repeatedly. >> Progress! >> >> This isn't exactly what you want to do, but leads me to believe that, >> with the proper tools, it may not be as difficult as I imagined. (famous >> last words...) >> >> Best, >> Frank >> >> >> ------------------------------------------------------------------------------ >> Enter the BlackBerry Developer Challenge >> This is your chance to win up to $100,000 in prizes! For a limited time, >> vendors submitting new applications to BlackBerry App World(TM) will have >> the opportunity to enter the BlackBerry Developer Challenge. See full >> prize >> details at: http://p.sf.net/sfu/Challenge >> _______________________________________________ >> Nasm-users mailing list >> Nas...@li... >> https://lists.sourceforge.net/lists/listinfo/nasm-users >> >> > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24422303.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: zak100 <zul...@ya...> - 2009-07-12 13:19:15
|
I have also created a stack but its not detected by the linker. D:\nasm prog>alink -o kernel.exe test_kernel2.obj main1.o ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. All Rights Reserved Loading file test_kernel2.obj Loading file main1.o matched Externs matched ComDefs Warning - no stack D:\nasm prog> CLI ;Clear Interupt Flag so while setting ;up the stack any interrupt would not be fired. mov AX,7B0h ;lets have the stack start at 7c00h-256 = 7B00h mov SS,ax ;SS:SP = 7B0h:256 = 7B00h:256 mov SP,256 ;Lets make the stack 256 bytes. ; Mov ax,CS ;Set the data segment = CS = 0 ; suggest you use 0 here! XOR AX,AX ;Makes AX=0. MOV ES,AX ;Make ES=0 mov DS,ax STI ;Set Back the Interupt Flag after ;we finished setting a stack frame. zak100 wrote: > > Hi, > > The changes for jump code lies here: > > GiveControlToOS: ; PROC > > LEA AX,[szDone] > Call PrintMessage > CALL GetKey > > ; db 0e9h ;Far JMP op code. > ; dw 512 ;JMP 512 bytes ahead > ;jmp 0:7E00h;-----------------------------------At this point we are > going to Kernel code; for including C prog:test_kernel2.asm > > ;jmp 7D0h:100h > jmp 0:7D00h > ; POP AX ;//Another why to make > ;the CPU jump to a new place. > ; POP AX > ; Push 7E0h ;Push New CS address. > ; Push 0 ;Push New IP address. > ;The address that comes out is 7E00:0000. > ; comment does not match code! > > > ;(512 bytes Higher from were BIOS Put us.) > retf > ; you'd want "retf" here. > > ;GiveControlToOS EndP > > The changes for ..start lie here > > ..start: > extern _K_main > ;--- test kernel.bin--- > > ; push cs > ; push cs > ; pop ds > ; pop es > mov ax, 7E0H > mov ds,ax > mov es,ax > mov si, msg > > mov ah, 0Eh > mov bx, 7 > top: > lodsb > cmp al, 0 > jz blackhole > int 10h > jmp short top > > blackhole: > > mov cx,1000 > Delay: Loop Delay > call _K_main; this is in main.c > hlt > jmp blackhole > > msg db 'Welcome to the Kernel!', 0 > > times 0x200-($-$$) db 0 > > Kindly help me with this. > > > Zulfi. > > Frank Kotler-3 wrote: >> >> zak100 wrote: >>> Hi, >>> I am able to make the binary using alink. partcopy was giving prob but >>> it >>> was handled as done previously. >>> >>> The steps are provided below: >>> D:\nasm prog>nasm -f bin -o boot.bin BLKernelT.asm >>> >>> D:\NASMPR~1>nasm -f bin -o writeit.com writeit.asm >>> >>> D:\NASMPR~1>writeit >>> >>> D:\NASMPR~1>del test_kernel2.onj >>> Could Not Find D:\NASMPR~1\test_kernel2.onj >>> >>> D:\NASMPR~1>del test_kernel2.obj >>> >>> D:\NASMPR~1>nasm -f obj test_kernel2.asm >>> >>> D:\NASMPR~1>dir t*.obj >>> Volume in drive D is New Volume >>> Volume Serial Number is 70FD-1D2C >>> >>> Directory of D:\NASMPR~1 >>> >>> 07/10/2009 10:55 AM 642 test_kernel2.obj >>> 1 File(s) 642 bytes >>> 0 Dir(s) 22,656,360,448 bytes free >>> >>> D:\NASMPR~1>alink -o kernel.bin test_kernel2.obj main1.o >>> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >>> All Rights Reserved >>> >>> Loading file test_kernel2.obj >>> Loading file main1.o >>> matched Externs >>> matched ComDefs >>> Warning, no entry point specified >> >> You can fix this by adding a "..start:" label where you want your >> entrypoint. >> >>> Warning - no stack >> >> segment stack stack >> resb ???? ; how big do you want? C uses a lot of stack, I understand. >> >> The first "stack" is just a name - could be anything. The second "stack" >> is necessary to let Nasm know what we want. I've been ignoring the >> warning, and "let the OS take care of the stack". We set up a stack in >> the bootsector which should be "okay for now". When you get to task >> switching/multitasking, you'll want to swap stacks for each process (I >> imagine). >> >>> D:\NASMPR~1>alink -o kernel.exe test_kernel2.obj main1.o >>> ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams. >>> All Rights Reserved >>> >>> Loading file test_kernel2.obj >>> Loading file main1.o >>> matched Externs >>> matched ComDefs >>> Warning, no entry point specified >>> Warning - no stack >>> >>> D:\NASMPR~1>del kernel.bin >>> >>> D:\NASMPR~1>exe2bin kernel.exe kernel.bin >> >> Good. This produces a .com file (no matter what we call it) which >> expects to be loaded at 100h into... whatever segment. >> >>> D:\NASMPR~1>partcopy kernel.bin 0 200 -f0 200 >>> Failed to read source at offset 144 >>> D:\NASMPR~1>partcopy kernel.bin 0 144 -f0 200 >>> >>> D:\NASMPR~1> >>> ----------------------------------- >>> >>> But when I rebooted , I got boot sector statment but I am not seeing the >>> "welcome....." kernel message. >>> Can somebody plz guide me?? >> >> Try jumping to 7D0h:100h (assuming that you're writing this to 7E00h). >> >> I haven't done anything further with this. I've downloaded source (in >> assembly) for an exe2bin clone, thinking we might be able to modify it >> to produce a binary at "org 0", or perhaps create a loader for MZ to be >> included in our code. Only just begun to look at it. >> >> Tyler mentioned using bochs for testing. That would speed up development >> a *lot* (especially for me, since I don't like rebooting). I've >> downloaded bochs a couple of times, but never tried it. I've seen too >> many posts about "my bootsector works in bochs, but not in a real >> machine" (or vise versa), and "bochs is giving me this error message, >> what the heck does it mean?"... suggesting that bochs doesn't accurately >> reflect "real hardware" (can't match *all* of 'em, of course), and has a >> "learning curve" to it... >> >> Any tips, Tyler? Is bochs going to insist on seeing a full 1.44M image >> before it'll boot from a "fake floppy"? Well, I'll figure it out (or >> not), if and when I get around to trying it... >> >> Best, >> Frank >> >> >> ------------------------------------------------------------------------------ >> Enter the BlackBerry Developer Challenge >> This is your chance to win up to $100,000 in prizes! For a limited time, >> vendors submitting new applications to BlackBerry App World(TM) will have >> the opportunity to enter the BlackBerry Developer Challenge. See full >> prize >> details at: http://p.sf.net/sfu/Challenge >> _______________________________________________ >> Nasm-users mailing list >> Nas...@li... >> https://lists.sourceforge.net/lists/listinfo/nasm-users >> >> > > -- View this message in context: http://www.nabble.com/calling-%27C%27-functions-tp24404865p24448606.html Sent from the nasm-users mailing list archive at Nabble.com. |
From: Frank K. <fbk...@zy...> - 2009-07-15 09:52:33
Attachments:
mzboot.zip
|
zak100 wrote: > I have also created a stack but its not detected by the linker. Well, your loader doesn't pay any attention to where Alink would say the stack was, anyway. You may be better off to "let the OS take care of the stack" like we do in real OSen. You set up a stack in your bootsector - not very big but it should do for now. Easily made bigger. Since I haven't got exe2bin on Linux, I tried loading the MZ executable that Alink produces. This is very rudimentary - won't load a very complicated MZ file. I've been ignoring the warning about "no stack" - I'm afraid even adding a stack will complicate the MZ too much for this feeble effort, so no stack (I use yours), and no "data" segment declared - everything's in "segment code". "build" won't work for you, of course... Slow going. I'm tired. I don't even know if I can attach a file to this list. Try it. Later, Frank |