I am trying to write a homework assignment which is to:
write a simple Assembly program that all it does is call a C program, and send to it the command line arguments so that it may run properly with (argc and argv).
How can this be done? We were given this asm as part of the assignment:
section .text
global _start
extern main
_start:
;;code to setup argc and argv for C program's main()
call main
mov eax,1
int 0x80
So what I want to know is, where are argc
and argv
located? Also, do I just need to put the pointer to argc
in the eax
register like when returning a value to a regular C function and the C program will work the rest out?
In the end, after compiling my C program with the following Makefile (as I said, I am new to Assembly and this is the Makefile given to us by the teacher, I do not fully understand it):
%.o: %.asm
nasm -g -O1 -f elf -o $@ $<
%.o: %.c
gcc -m32 -g -nostdlib -fno-stack-protector -c -o $@ $<
all: lwca
lwca: lwc.o start.o
ld -melf_i386 -o $@ $^
Running ./lwca arg1 arg2
should result in argc = 3
and argv[1]=arg1 argc[2]=arg2
ANSWER: No answer quite solved my problem, in the end the what worked was:
pop dword ecx ; ecx = argc
mov ebx,esp ; ebx = argv
push ebx ; char** argv
push ecx ; int argc
call main