ASM Code:
format PE console ;Format PE OUT GUI 4.0
entry main
include 'macro/import32.inc'
section '.idata' import data readable ;Import Section.
library msvcrt,'msvcrt.dll'
import msvcrt, printf, 'printf',\
exit,'exit', getchar, 'getchar', scanf, 'scanf'
section '.data' data readable writeable ;Constants/Static Section. This section can store variables.
Size dd 0 ;Array Size. A Dword Initialized to 0
PromptValue db "Enter A Number: ", 13, 10, 0 ;Prompt User For A Number.
FuncPrint db 13, 10, 13, 10, "Calling Function: %s", 13, 10, 0 ;Statement For Function.
Response db 13, 10, 13, 10, "You Entered: %d", 13, 10, 0 ;Statement For Number.
InputFormat db "%d", 0 ;Format Data
IntArray dd 0 ;Declare Dword Initialized to 0
Index dd 0
section '.code' code readable executable
main:
push ebp
mov ebp,esp
push PromptValue ;Ask the user to Enter a number.
call [printf]
add esp, 4
push Size
push InputFormat
call [scanf] ;Call scanf with the "%d" and the variable to hold the integer.
add esp, 8
push [Size]
push Response
call [printf] ;Print the value the user entered with "%d".
add esp, 8
mov ecx, [Size] ;Store the Array Size/Number in the count register.
mov [Index], 0 ;Set the Index variable to 0.
ASSIGN: ;The beginning of a loop.
push ecx ;Backup our count because printf discards it.
mov ecx, [Index] ;Move our index to the new count register.
inc [Index] ;Increment the index.
mov [IntArray + 4 * ecx], ecx ;Change the value at the array's index to the current count.
pop ecx ;Restore our old count.
loop ASSIGN ;Loop if Count is > 0.. else break.
;TODO: Add Loop to print array..
mov [Index], 65 ;Store the character A in variable called Index.
push Index ;Pass Index to the function as a parameter by pushing it onto the stack.
call Function ;Call the function.
add esp, 4 ;Restore the stack pointer..
mov esp, ebp
pop ebx
call [getchar] ;Wait until the user presses a key before we exit.
call [getchar]
mov eax, 0 ;Return 0.
ret
Function:
push ebp
mov ebp, esp
mov eax, [ebp + 8] ;Function Parameter
push eax
push FuncPrint
call [printf] ;Print the Parameter Passed..
add esp, 8
mov esp, ebp
pop ebp
ret