; cmdline -- write command line arguments to stdout ; Copyright (C) 2004 Steffen Solyga ; ; This program is free software; you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation; either version 2 of the License, or ; (at your option) any later version. ; ; This program is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License ; along with this program; if not, write to the Free Software ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ; $Id: cmdline.asm,v 1.2 2004/04/24 01:09:00 solyga Exp $ %define fd_stdout 1 %define sys_exit 1 ;ebx: errno %define sys_write 4 ;ebx: fd, ecx: str, edx: len section .data ;section declaration new_line db 0x0a ;new line string7 db '____: ',0 ;number arg_index dd 0x0 ;argument index section .text ;section declaration global _start ;linker entry point (ld -e overrides) ;command line arguments are passed via the stack, i.e. ;[esp+0]: number of arguments (argc) ;[esp+4]: address of first arg (argv[0]) ;[esp+n]: NULL ;[esp+n+4]: environment ;[esp+n+m]: NULL _start: ;[esp]: argc, [esp+4]: argv[0] mov ebp,esp ;save esp (needed for argc) mov eax,0 out_arg: mov [arg_index],eax ;update argument index call out_dword_hex add ebp,4 ;next argument mov eax,[ebp] ;load string addr call out_str call out_nl mov eax,[arg_index] inc eax cmp eax,[esp] ;argc reached? jnz out_arg exit: mov eax,sys_exit mov ebx,0 ;exit code int 0x80 ;#################### ; function: out_str - sys_writes 0-termed string to stdout ; params: eax - address of first char ; mods: eax,ebx,ecx,edx out_str: ;eax: str mov ecx,eax xor edx,edx cnt_chars: ;ecx: str, edx: 0 cmp byte [ecx+edx],0 jz cnt_done inc edx jmp cnt_chars ;.................... cnt_done: ;ecx: str, edx: len mov eax,sys_write mov ebx,fd_stdout int 0x80 ret ;#################### ;function out_nl - sys_writes newline to stdout ; params % ; mods eax,ebx,ecx,edx out_nl: mov eax,sys_write ;output newline mov ebx,fd_stdout mov ecx,new_line mov edx,1 ;len int 0x80 ret ;#################### ; function: out_dword_hex ; params: eax ; mods: eax,ebx,ecx,edx out_dword_hex: mov ecx,4 ;char counter out_dword_hex_cont: mov ebx,eax ;work on ebx and bl,0x0f or bl,0x30 cmp bl,0x3a ;bl < 0x3a ? done : +0x27 jc out_dword_hex_done add bl,0x27 out_dword_hex_done: mov [string7-1+ecx],bl shr eax,4 dec ecx jnz out_dword_hex_cont mov eax,string7 call out_str ret ;####################