initial import
[staff/goc4/2018bti7061.git] / asm-8-strings / examples / showargs / showargs1.asm
1 ; Executable name : SHOWARGS1
2 ; Version : 1.0
3 ; Created date : 4/17/2009
4 ; Last update : 5/19/2009
5 ; Author : Jeff Duntemann
6 ; Description : A simple program in assembly for Linux, using NASM 2.05,
7 ; demonstrating the way to access command line arguments on the stack.
8 ;
9 ; Build using these commands:
10 ; nasm -f elf -g -F stabs showargs1.asm
11 ; ld -o showargs1 showargs1.o
12 ;
13
14 SECTION .data ; Section containing initialised data
15
16 ErrMsg db "Terminated with error.",10
17 ERRLEN equ $-ErrMsg
18
19 SECTION .bss ; Section containing uninitialized data
20
21 ; This program handles up to MAXARGS command-line arguments. Change the
22 ; value of MAXARGS if you need to handle more arguments than the default 10.
23 ; In essence we store pointers to the arguments in a 0-based array, with the
24 ; first arg pointer at array element 0, the second at array element 1, etc.
25 ; Ditto the arg lengths. Access the args and their lengths this way:
26 ; Arg strings: [ArgPtrs + <index reg>*4]
27 ; Arg string lengths: [ArgLens + <index reg>*4]
28 ; Note that when the argument lengths are calculated, an EOL char (10h) is
29 ; stored into each string where the terminating null was originally. This
30 ; makes it easy to print out an argument using sys_write. This is not
31 ; essential, and if you prefer to retain the 0-termination in the arguments,
32 ; you can comment out those lines as indicated.
33
34 MAXARGS equ 10 ; Maximum # of args we support
35 ArgCount: resd 1 ; # of arguments passed to program
36 ArgPtrs: resd MAXARGS ; Table of pointers to arguments
37 ArgLens: resd MAXARGS ; Table of argument lengths
38
39 SECTION .text ; Section containing code
40
41 global _start ; Linker needs this to find the entry point!
42
43 _start:
44 nop ; This no-op keeps gdb happy...
45
46 ; Get the command line argument count off the stack and validate it:
47 pop ecx ; TOS contains the argument count
48 cmp ecx,MAXARGS ; See if the arg count exceeds MAXARGS
49 ja Error ; If so, exit with an error message
50 mov dword [ArgCount],ecx ; Save arg count in memory variable
51
52 ; Once we know how many args we have, a loop will pop them into ArgPtrs:
53 xor edx,edx ; Zero a loop counter
54 SaveArgs:
55 pop dword [ArgPtrs + edx*4] ; Pop an arg into the memory table
56 inc edx ; Bump the counter to the next argument
57 cmp edx,ecx ; Is the counter = the argumemt count?
58 jb SaveArgs ; If not, loop back and do another
59
60 ; With the argument pointers stored in ArgPtrs, we calculate their lengths:
61 xor eax,eax ; Searching for 0, so clear AL to 0
62 xor ebx,ebx ; Pointer table offset starts at 0
63 ScanOne:
64 mov ecx,0000ffffh ; Limit search to 65535 bytes max
65 mov edi,dword [ArgPtrs+ebx*4] ; Put address of string to search in EDI
66 mov edx,edi ; Copy starting address into EDX
67 cld ; Set search direction to up-memory
68 repne scasb ; Search for null (0 char) in string at edi
69 ; Comment out the following line if you need mull-terminated arguments:
70 mov byte [edi-1],10 ; Store an EOL where the null used to be
71 sub edi,edx ; Subtract position of 0 from start address
72 mov dword [ArgLens+ebx*4],edi ; Put length of arg into table
73 inc ebx ; Add 1 to argument counter
74 cmp ebx,[ArgCount] ; See if arg counter exceeds argument count
75 jb ScanOne ; If not, loop back and do another one
76
77 ; Display all arguments to stdout:
78 xor esi,esi ; Start (for table addressing reasons) at 0
79 Showem:
80 mov ecx,[ArgPtrs+esi*4] ; Pass offset of the message
81 mov eax,4 ; Specify sys_write call
82 mov ebx,1 ; Specify File Descriptor 1: Standard Output
83 mov edx,[ArgLens+esi*4] ; Pass the length of the message
84 int 80H ; Make kernel call
85 inc esi ; Increment the argument counter
86 cmp esi,[ArgCount] ; See if we've displayed all the arguments
87 jb Showem ; If not, loop back and do another
88 jmp Exit ; We're done! Let's pack it in!
89
90 Error: mov eax,4 ; Specify sys_write call
91 mov ebx,1 ; Specify File Descriptor 2: Standard Error
92 mov ecx,ErrMsg ; Pass offset of the error message
93 mov edx,ERRLEN ; Pass the length of the message
94 int 80H ; Make kernel call
95
96 Exit: mov eax,1 ; Code for Exit Syscall
97 mov ebx,0 ; Return a code of zero
98 int 80H ; Make kernel call