x86 Assembly Command Line Arguments
Posted: Thu Jan 12, 2012 10:21 am
Hello, I've written a few assembly programs recently trying to print out all of the command line arguments given to the program but I've come across a few issues. First way I tried (just to get the first argument) was to pop off the char* and try to print it with the system call write:
But the problem with it was that I didn't know how long each of the argument strings were, and the write system call doesn't stop writing after hitting zero, and I'm not sure that the arguments are zero terminated.
So now I've tried this:
Where I take the first 'real' command line argument (doesn't really matter which one I guess) and go through it character by character until I hit zero. The code I have above always fails with a segmentation fault in printargv between .start and .done.
The way I'm doing this probably looks weird, but thats because I'm adapting some code that talks to the BIOS to write code out character by character. How can I go through a string character by character in 32-bit code? Is there a better way to print out the command line arguments using assembly?
Just in case it's useful to anyone this is using the nasm assember.
Code: Select all
section .text
global _start
_start:
pop ecx
pop ecx ; something along the lines of ./a.out
mov eax, 4
mov ebx, 1
mov edx, 20
int 0x80
...
So now I've tried this:
Code: Select all
section .text
global _start
_start:
pop eax
call printargv
mov eax, 1
mov ebx, 0
int 0x80
printargv:
pop esi
pop esi
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msglen
int 0x80
.start
lodsb
or al, al
jz .done
mov ebx, 1
mov ecx, eax
mov eax, 4
;mov byte [buffer], al
;mov ecx, [buffer]
mov edx, 1
int 0x80
jmp .start
.done
ret
section .bss
buffer resb 1
section .data
msg db 'Entering printargv...', 13, 10, 0
msglen equ $-msg
The way I'm doing this probably looks weird, but thats because I'm adapting some code that talks to the BIOS to write code out character by character. How can I go through a string character by character in 32-bit code? Is there a better way to print out the command line arguments using assembly?
Just in case it's useful to anyone this is using the nasm assember.