Jump to content

[X86 ASM] Command line arguments demo

- - - - -

  • Please log in to reply
No replies to this topic

#1
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
Figuring out command line arguments in relation to C in assembly gave me trouble back when I first started assembly so I figured I'd post this incase someone runs into the same problems.

[section .text]

global _main

extern _printf


_main:

	push dword [esp + 4] ;argc is located at esp+4

	push fmt1

	call _printf

	add esp, 8


	mov eax, dword [esp+8]	;pointer to argv[0] is located at esp+8

	push dword [eax]		;argv[0] pointer

	push fmt2

	call _printf

	add esp, 8

	

	mov eax, dword [esp+8] ;pointer to argv[0]

	mov edx, dword [eax]	;argv[0]

	push dword [edx]		;argv[0][0] or [edx+1] for next character

	push fmt3

	call _printf

	add esp, 8

	

	mov eax, dword [esp+8]	;pointer to argv[0]

	push dword [eax+4]		;move to next pointer / argv[1]

	push fmt4

	call _printf

	add esp, 8

	

	mov eax, 0

	ret

	



[section .data]

fmt1 db 'argc = %d', 10, 0

fmt2 db 'argv[0] =  %s', 10, 0

fmt3 db 'argv[0][0] = %c', 10, 0

fmt4 db 'argv[1] = %s', 10, 0


[section .bss]

If you run into problems figuring out the **argv like I did just reread this part

	mov eax, dword [esp+8]	;pointer to argv[0] is located at esp+8

	push dword [eax]		;argv[0]

	push fmt2

	call _printf

	add esp, 8






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users