; Filename: lat.asm
; Developer: yudi
; Date: 06 mar 2012
; Purpose: This program demonstrates
; procedural programming and
; the usage of environment
; variables.
; Build:
; nasm -f elf -o lat.o lat.asm
; gcc -o lat lat.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
pelajari sendiri ya dan jalankan di linux anda