Loops, Arrays and Subroutines| Stacks and Parameters

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

flashcard set

Earn XP

Description and Tags

Lecture 5 & 6

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards
What are loops for?
\-Loops are used to iterate over instructions by jumping backwards in the code. Jump instructions move the instruction pointer back to an earlier point in the code.

\-Loops need to have terminating conditions - they iterate while a condition is true or until a certain condition is true.
2
New cards
What are the different types of loops?
\-While loop

\-Do While loop

\-For loop
3
New cards
How is ECX used in loops?
The loop instruction performs a jump based on the value of ECX.

\-First it decrements ECX.

\-Then jumps to the given label if ECX is not zero.
4
New cards
How can we get the address of a variable?

(Load effective address)
lea ebx, age
5
New cards
What is used to access the memory address stored in a register?
Register Indirect mode.

mov eax, \[ebx\]
6
New cards
What are arrays used for?
Arrays are items stored in consecutive memory locations. The amount of memory depends on the data being stored in the array.
7
New cards
How much memory does an array take up in a 32-bit system?
Four bytes of memory
8
New cards
How do we find the memory address of an array?
How do we find the memory address of an array?
lea ebx, grades
9
New cards
What are subroutines?
Subroutines are sections of code which can be invoked repeatedly as the program executes.
10
New cards
Why do we use subroutines?
* Save effort in programming.
* Reduce the size of programs.
* Share code.
* Encapsulate or package a specific activity.
* Provide easy access to tried and tested code.
11
New cards
How are subroutines used in asm?
Simple jump to a label (address) that points to the first instruction in the subroutine. Use the **call** instruction with the label of the subroutine. Use the **ret** instruction to return from the subroutine.
12
New cards
Explain the RET instruction
\
* When a subroutine is called, the instruction pointer is changed to its address. Fetch-execute cycle continues with instructions from that point onward.
* The ret instruction changes the instruction pointer back to the address following the original call instruction. The CPU needs a way to remember where to return to.


1. Pops top item off stack and places it into the IP.
2. Next instruction to be fetched will be the instruction after original call instruction.
13
New cards
What are stacks used for?
Stacks allow us to store an arbitrary number of nested return addresses. It uses main memory of the current process. It is treated separately and accessed directly through CPU registers.

*Stack grows downwards in memory.*
14
New cards
Explain ESP, PUSH and POP
ESP - stack pointer, always points to memory address of top item on the stack.

PUSH – decrements ESP, points to next free area of memory. Writes the data item to that address.

POP – moves the data addressed by ESP into given register. Increments ESP by the correct amount to remove the item from stack.
15
New cards
Explain the CALL instruction

1. Records the current value of the IP and pushes it onto the stack.
2. Puts the address of subroutine into the IP.
3. Next instruction to be executed is the first instruction of the subroutine.
16
New cards
How do you adjust the stack?
* The locations increase by 4-bytes each time due to 32-bit word length.
* Items can be removed from the stack, or space can be reserved on top by altering the stack pointer.
* Example – to take 8 bytes off stack: add esp, 8
* Example – to reserve space on top of stack for 256 bytes: sub esp, 256
17
New cards
What are the two forms of parameters?
* **Value parameters:** the additional information will be some static value, if function uses a value, then we are **passing by value.**
* **Reference parameters:** these are the addresses of variables and not the values themselves. Known as **passing by reference.**
18
New cards
What does Pass by Value depend on?
What does Pass by Value depend on?
The caller and the callee agreeing on which registers to use.
19
New cards
What are the four calling conventions?
* cdecl – pushes parameters on stack in reverse order (right to left), caller cleans stack.
* fastcall – first two parameters in registers, rest reversed on stack, callee cleans stack.
* stdcall – pushes parameters on stack in reverse order, callee cleans stack.
* thiscall – first parameter in ECX, rest reversed on stack, callee cleans.