; Title: CISS 360 Proc Demo 1
;------------------------------------------------------------
; Description: This program demonstrates how a proc is called
; and how a proc accesses parameters passed to it.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Thursday, January 27, 2011 4:05 PM
;------------------------------------------------------------
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h ; header file for console input/output
.STACK 4096 ; reserve 4096-byte (1024 DWORD) stack
;------------------------------------------------------------
; Input/Output Facility
;------------------------------------------------------------
; Macro Operand
;
; put_ch DWORD/Reg (Displays character in parameter)
; get_ch None (Loads character from keyboard into EAX)
; put_str BYTE (Displays string beginning at given address)
; put_i DWORD/Reg (Displays signed integer value in parameter)
; put_u DWORD/Reg (Displays unsigned integer value in parameter)
; put_fp REAL4/Reg (Displays float value in parameter)
; get_i DWORD (Loads signed integer entered at keyboard into parameter)
; get_u DWORD (Loads unsigned integer entered at keyboard into parameter)
; get_fp REAL4 (Loads float entered at keyboard into parameter)
; get_str BYTE (Loads string entered at keyboard into parameter)
;------------------------------------------------------------
.DATA ;****************************************************
;* Data Section *
;****************************************************
x DWORD ?
y DWORD ?
Sum DWORD ?
prompt_1 BYTE "Enter first integer: ",0
prompt_2 BYTE "Enter second integer: ",0
result BYTE "Sum is ",0
newline DWORD 10
.CODE ;****************************************************
;* Code Section *
;****************************************************
_MainProc PROC
rerun_main:
put_str prompt_1 ; prompt for 1st integer value
get_i x ; get 1st integer value
put_str prompt_2 ; prompt for 2nd integer value
get_i y ; get 2nd integer value
push y ; push 2nd parameter
push x ; push 1st parameter
call Adder ; invocation: Adder (x, y)
add esp, 8 ; clean up stack
mov Sum, eax ; save sum (i.e., value "returned" by proc)
put_str result ; display result label
put_i Sum ; display sum
put_ch newline ; display newline
put_ch newline ; display another newline
done ; display completion message
xor eax, eax ; exit with return code 0
ret
main_done:
done ; display completion/do-again message
cmp eax, 0 ; EAX = 0?
je rerun_main ; yes, go run program again
xor eax, eax ; exit with return code 0
ret
_MainProc ENDP
;------------------------------------------------------------
Adder PROC ; sample procedure
push ebp ; save base pointer
mov ebp, esp ; establish stack frame
;; push registers used by this proc N/A
;; proc code inserted here
mov eax, 0 ; initialize accumulator
add eax, DWORD PTR [ebp+8] ; add 1st parameter
add eax, DWORD PTR [ebp+12] ; add 2nd parameter
;; pop registers used by this proc N/A
pop ebp ; restore base pointer
ret
Adder ENDP
;----------------------------------------------------------------------
stat_flags MACRO
fstsw ax ; FPU status word to AX
sahf ; condition code bits to flags
ENDM
;----------------------------------------------------------------------
END ; end of source code
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.