Commands
Name / Parameters / ResultfillAccount / 1. address of account
2. account number / fills account with random info
fillAccounts / accounts address / fills all accounts with random info
checkAccount / account address(7.2) / displays info for account
allocate / size in bytes(eg. TYPEOF array) / address of memory goes into eax
release / memory address / releases memory to be overwritten
blockSize / memory address / size of block in eax
memoryStatus / - / Shows if any memory is currently being used
push / <src> / pushes a 32 bit word onto the stack
pop / <dst> / pops off a 32bit word from the stack
readRPN / - / String address: EAX
checkRPN / value of last string / -
call / <label> / goes into a subroutine called <label>
ret / - / returns from a subroutine
ret / n / returns from a subroutine and goes back down the stack n number of bytes
(n = 4 * parameters pushed)
leave / - / -
Using separate files
● right click “source files”, add, new item and name it whatever.asm
● to save time copy everything out of main and paste it into the new file
● replace “end main” at the end of the new file with just “end”
● just under the includes add EXTERNDEF sum:NEAR replacing “sum” with the NAME OF YOUR METHOD AND NOT THE FILE ITSELF
● push the appropriate amount of things onto the stack and call your method:
eg.
push OFFSET array
push LENGTH array
call sum
● use the preamble in every one of your methods:
sum:
push ebp
mov ebp,esp
mov ebx, [ebp + 8] ⇒ Access variable that was push on last
mov ecx, [ebp + 12] ⇒ Access variable that was pushed on first
(Have as many moves as you have variables, goes up in 4s eg. EBP + 16 etc.)
pop ebp
ret 8 (ret 4 * the amount of elements you pushed on)
*BOLD TEXT IS REQUIRED*
● If you’re returning a value then return it to eax at the end
Arrays in MASM
a DWORD 0
x DWORD 0,0,0,0,0
x DWORD 5 DUP(0)
y DWORD 5 DUP(0,1,2)
You can use the first or last with an exact count of how many are there
eg.
z DWORD 5 DUP(1,2,3,4,5)
mov eax, sizeOf array / type array - Moves the amount of elements of the array to eax
LENGTH array - Gets the length of Array called “array”
OFFSET array - Gets the address of the first element in an Array called “array”
TYPEOF array - gets size of array element (eg. DWORD array returns 4 here)
[eax] - takes the value stored in eax and treats it as an address, takes value from eax-
Going through array
mov ebx, LENGTH array
mov ecx, OFFESET array
startLoop:
cmp ebx,count compare size of array to count variable
je endLoop jump to end of loop
mov eax,TYPEOF array (or 4) moves the size of array element into eax
mul count multiply eax by count ⇒ 4 * i
add eax,ecx add start address to eax ⇒ start + (4 * i)
mov eax, [eax] mov value in address eax into eax
inc count increments count
jmp startLoop goes back to top
endLoop:
you can do whatever you want using the value you get into eax at the end. The structure is the same if it’s in a separate methodjust push the length and address onto the stack and move them into ebc and ecx instead:
mov ebx, [ebp + 8]
mov ecx, [ebp + 12]
then all the other separate method preamble
Structures in MASM
Linked lists not on test but a great example for structures
Creating and Accessing
● put structure under include files in the format below
LinkedList STRUCT
value SDWORD 0
next DWORD 0
LinkedList ENDS
● When making a new iteration of the structure you need to allocate memory for it using the code below:
invoke allocate,SIZEOF LinkedList
⇒ This gets a free block of memory and returns the memory location to eax
⇒ Always keep the value of the current address in a variable
● You can access the values in a structure like so:
mov [eax].LinkedList.value,ebx ⇒ moves value into eax
mov [eax].LinkedList.next,eax ⇒ moves new address of next node from eax
Separate Files
● All you need to push with a structure generally is the address of the structure itself. The values can be accessed the same as normal then
eg.
mov eax, [ebp+8]
mov eax,[eax].LinkedList.value
Release Memory Of Structure
releaseList:
cmp list,0
je releaseListEnd
mov eax,list
mov ebx,[eax].LinkedList.next
mov list,ebx
invoke release,eax
jmp releaseList
releaseListEnd:
The process is the same for every structure though you might not need the loop, all you need is just to use release on the address of the structure.