From: Frank K. <fbk...@co...> - 2005-03-15 23:50:45
|
Matthias-Christian Ott wrote: > > Hi! > I've 2 Problems with nasm: > > 1: *Strage Linking Problem* > > I have this code: > > SECTION .DATA > hello: db 'Hello world!',10 > helloLen: equ $-hello > > SECTION .TEXT > GLOBAL main > > main: > ; Write 'Hello world!' to the screen > mov eax,4 ; 'write' system call > mov ebx,1 ; file descriptor 1 = screen > mov ecx,hello ; string to write > mov edx,helloLen ; length of string to write > int 80h ; call the kernel > > ; Terminate program > mov eax,1 ; 'exit' system call > mov ebx,0 ; exit with error code 0 > int 80h ; call the kernel > > If try to assemble, link (by using ld) and run it, I only get a > segementation fault (I have to rename main to _start): "_start" is the default entrypoint known to ld (should be declared "global" so ld can "see" it). You can tell ld to use a different symbol (or an address, I guess) as the entrypoint with "--entry=main". > nasm -f elf hello.asm > ld -s -o hello hello.o I'm surprised you don't get a complaint from ld here! > ./hello > segementation fault > > If I try to link it with the gcc (_start -> main), everything works fine. C knows "main" as the entry into *your* part of the code, and it's "call"ed, so you can end with "ret". In this case the "_start" label occurs in the "C startup code", which diddles the stack, among other things, and then calls "main". > What's wrong? Why doesn't it work? Just that you didn't use "_start" or tell ld another entrypoint. > 2: *Strange Syntax Error* > > I have this code (I know that it will only work in the priviledged mode, > but it should assemble cleanly (?)): > > global sys_core_in > > sys_core_in: > push ebp > push dx This will work, but you probably want to push/pop edx here. It'll generate shorter code, and leave your stack aligned better. > mov ebp,esp > in edx,[ebp+4] <http://nasm.sourceforge.net/doc/html/nasmdocb.html#section-B.4.119> > mov eax,dx I don't think you'll need to do this, but... <http://nasm.sourceforge.net/doc/html/nasmdocb.html#section-B.4.156> ... or use "movzx"... but I don't think you need it... > pop dx > pop ebp > ret > > global sys_core_out > > sys_core_out: > push ebp > mov ebp,esp > out [ebp+4],[ebp+8] <http://nasm.sourceforge.net/doc/html/nasmdocb.html#section-B.4.194> > pop ebp > ret > > If I try to assemble it I get this: > > io.s:7: error: invalid combination of opcode and operands > io.s:8: error: invalid combination of opcode and operands > io.s:18: error: invalid combination of opcode and operands > > What's wrong with the syntax? Just like what the error message says - the operands you're using aren't valid for the opcodes. I don't know anything about "privileged mode", but you can get "permission" to in/out ports with "sys_ioperm"... might be useful for testing(?). Best, Frank P.S. I've removed "nasm-devel" from the reply... not really a "development issue"... |