Thanks, Bryant! Always looking for examples. Pity we're not going to see the tutorial!
I haven't had much time to look at 'em. By ex02 I'm confused, and by ex03, completely baffled!
; Filename: ex03.asm
; Developer: Bryant Keller
; Date: 05 Sep 2009
; Purpose: This program demonstrates
; procedural programming and
; the usage of environment
; variables.
; Build:
; nasm -f elf -o ex03.o ex03.asm
; gcc -o ex03 ex03.o
BITS 32
GLOBAL main
EXTERN puts
EXTERN exit
%define @ARG EBP + 8
%define @VAR EBP - 8
SECTION .data
strEnvironment DB '-----------------------', 10
DB ' Environment Variables', 10
DB '-----------------------', 0
strArguments DB '------------------------', 10
DB ' Command Line Arguments', 10
DB '------------------------', 0
SECTION .text
envs:
STRUC @ENVP
.envc RESD 1
.envp RESD 1
ENDSTRUC
STRUC $ENVP
.ptr RESD 1
ENDSTRUC
PUSH EBP
MOV EBP, ESP
SUB ESP, $ENVP_size
PUSH DWORD strEnvironment
CALL puts
MOV ESI, [@ARG + @ENVP.envp]
MOV ECX, [@ARG + @ENVP.envc]
DEC ECX
MOV EAX, 4
.scan_envs:
PUSH ECX
CMP ECX, 0
JE .done
DEC ECX
MOV EBX, [ESI + EAX]
OR EBX, EBX
JE .done
ADD EAX, 4
MOV [@VAR - $ENVP.ptr], EAX
PUSH EBX
CALL puts
MOV EAX, [@VAR - $ENVP.ptr]
POP ECX
JMP .scan_envs
.done:
XOR EAX, EAX
LEAVE
RET
args:
STRUC @ARGS
.argc RESD 1
.argv RESD 1
ENDSTRUC
STRUC $ARGS
.ptr RESD 1
ENDSTRUC
PUSH EBP
MOV EBP, ESP
SUB ESP, $ARGS_size
PUSH DWORD strArguments
CALL puts
ADD ESP, 4
MOV ESI, [@ARG + @ARGS.argv]
MOV ECX, [@ARG + @ARGS.argc]
DEC ECX
MOV EAX, 4
.scan_args:
PUSH ECX
CMP ECX, 0
JE .done
DEC ECX
MOV EBX, [ESI + EAX]
OR EBX, EBX
JE .done
ADD EAX, 4
MOV [@VAR - $ARGS.ptr], EAX
PUSH EBX
CALL puts
ADD ESP, 4
MOV EAX, [@VAR - $ARGS.ptr]
POP ECX
JMP .scan_args
.done:
XOR EAX, EAX
LEAVE
RET
main:
STRUC ENV
.argc RESD 1
.argv RESD 1
.envp RESD 1
ENDSTRUC
PUSH EBP
MOV EBP, ESP
PUSH DWORD [@ARG + ENV.argv]
PUSH DWORD [@ARG + ENV.argc]
CALL args
MOV ESI, [@ARG + ENV.envp]
MOV ECX, 4
XOR EAX, EAX
.continue:
MOV EBX, [ESI + ECX]
OR EBX, EBX
JE .count_done
ADD ECX, 4
INC EAX
JMP .continue
.count_done:
PUSH DWORD [@ARG + ENV.envp]
PUSH EAX
CALL envs
XOR EAX, EAX
PUSH EAX
CALL exit
LEAVE
RET
Sometimes you restore the stack after "puts", sometimes not. In particular, in your ".scan_envs:" loop...
PUSH EBX
CALL puts
MOV EAX, [@VAR - $ENVP.ptr]
POP ECX
ECX used to hold the count of environment variables, lovingly acquired in main. I don't think it does, anymore. It works anyway, because of the other "bailout"... which exits the loop with a register (or two! in this case) on the stack. LEAVE saves your asm, of course. I hope this "deferred cleanup" is explained in the tut, you do it a lot.
Then, in ex07...
%define PTR &
Aw, maaaan! After teaching the kiddies sane syntax, you revert to this unspeakable line-noise? Aw.... maaaan...!
Well, I'll look at it more later, but you seem to be heading in the "wrong direction" for me.
Best,
Frank