3410 Exam 3

Ace your homework & exams now with Quizwiz!

When a C function returns a value, which register is it stored in?

EAX

What are the 3 parts of a function?

Prologue - saves previous state and sets up stack for local variables Body - function body Epilogue - restores program to initial state

Which version of REP is the unconditional repeat?

REP repeats until CX is zero

How does REP work?

REP executes the instruction, decreases CX by 1, and checks whether CX is zero It repeats the instruction processing until CX is zero

CMPS

compares 2 data items in memory data could be of size byte, word, or doubleword

SCAS

compares the content of a register (AL, AX, or EAX) with the contents of an item in memory

What's the pitfall of dealing with C structures in assembly?

C pads the values unless otherwise told not to, for example: struct node { ........int id; ........char name[20]; ........struct node *next; } __attribute__((packed));

Which registers are used for string instructions?

ESI and EDI

MOVS

moves 1 byte/word/doubleword of data from memory to memory

STOS

stores data from register (AL, AX, or EAX) to memory

How to declare a string in assembly

string db 'my string', 0xa

Why do we call atoi()?

to convert a string to an integer

How would you copy a zero terminated string in assembly?

while the next byte of the source is not zero: ........copy the source byte to destination ........increment source index ........increment destination index put a zero byte at the end of the destination string

What are the suffices for the basic string instructions?

-B, -W, and -D -B for byte operation -W for word operation -D for doubleword operation Ex. CMPSW

CDECL standards for the CALLEE function (the one that's being called)

1. Push EBP onto stack, then copy value of ESP into EBP 2. Allocate local variables by making room onto the stack (decrement ESP depending on the number of local variables needed) 3. Save registers EBX, EDI, and ESI by pushing them onto the stack 4. Return value for function put in EAX 5. Restore old values for EBX, EDI, and ESI 6. Deallocate local variables by moving the value in EBP into ESP (mov ESP, EBP) 7. Pop EBP 8. Return to caller via "ret" instruction

CDECL standards for the CALLER function (the one doing the calling)

1. Save contents of EAX, ECX, and EDX by pushing them onto the stack 2. Push parameters onto the stack in inverted order 3. Use the "call" instruction to call a subroutine 4. Remove parameters from stack (restoring the stack to its state before the call was performed) 5. Caller can expect to find the return value in EAX 6. Caller restores contents of saved registers (EAX, ECX, and EDX) by popping them off the stack

Given the instructions below, what would be at the TOP of the stack? push 34 push 59 push 98

98

What does GLOBAL do?

Allows for a function to be accessed from anywhere in the assembly file ex. GLOBAL _start GLOBAL in NASM lets C code access assembly functions and global variables

What are the benefits of assembly compared to C?

Assembly takes up less space and is faster

What does the REP prefix do when set before a string instruction?

Causes repetition of the instruction based on a counter placed at the CX register Ex. REP MOVSB

What does CLD do?

Clear Direction Flag Sets direction flag to zero 0, making the operation left to right.

What's REPE/REPZ?

Conditional repeat (variation of REP) that repeats the operation while the zero flag indicates equal/zero. Stops when the zero flag indicates not equal/zero or when CX is zero.

What's REPNE/REPNZ?

Conditional repeat (variation of REP) that repeats the operation while the zero flag indicates not equal/zero. Stops when the zero flag indicates equal/zero or when CX is decremented to zero.

What does EXTERN do?

Designates a function as external, meaning it's not in the same file (ex. EXTERN printf, since printf is a C function used to print text to screen, and is not used by assembly) EXTERN used to access C functions and global variables

Which register is used to navigate the stack?

EBP points to the bottom of the stack

MOVS, LODS, STOS, CMPS, and SCAS use which pairs of registers?

ES:ESI and DS:EDI ESI normally associated with DS (data segment) EDI always associated with ES (extra segment)

T/F: a byte can be saved onto the stack

False

What would "sub esp, 16" do for us?

It would make empty space for local variables, moving ESP to a lower memory address

What are the drawbacks of assembly code?

It's hard to express stuff (duh)

What does the "lea" instruction do?

Load effective address Ex. lea eax, [edx + eax]

What's the difference between MOVSB, MOVSW, and MOVSD?

MOVSB is the byte operation MOVSW is the word operation MOVSD is the doubleword operation

Why do we "mov ebp, esp" in the function body?

Makes ESP point to the same spot as EBP. EBP is used to keep track of where the function started on the stack, and to navigate the stack. ESP will always point to the top of the stack.

How do you pop all registers?

POPA Pop DI, SI, BP, BX, DX, CX, and AX or POPAD Pop EDI, ESI, EBP, EBX, EDX, ECX, and EAX

How do you push all general registers?

PUSHA Push AX, CX, DX, BX, original SP, BP, SI, and DI or PUSHAD Push EAX, ECX, EDX, EBX, original ESP, EBP, ESI, and EDI

Which registers are used for implementing the stack?

SS and ESP (or SP), where the top of the stack is pointed to by SS:ESP. The SS register points to the beginning of the stack segment, and the SP (or ESP) gives the offset into the stack segment.

Why do we push ebp onto the stack in our function body?

Saves EBP on the stack, allowing us to use it as a checkpoint of sorts

What does STD do?

Set Direction Flag Sets direction flag to 1, making the operation right to left.

Size vs Length

Size - amount of memory Length - number of meaningful characters in a string

What is the stack used for?

Stores information about the function such as local variables and arguments, as well as the return address (so that the called function can return control to where it was called from)

Why would we use C with assembly?

To have the expressive power of C while also using the benefits of assembly as needed

Which direction does the stack grow?

Towards the lower memory address Top of stack points to lower byte of the last word inserted

T/F: call instruction pushes the return address onto the stack

True

T/F: functions are abstractions in assembly

True

T/F: strings are passed as pointers, with strings being character arrays

True

T/F: you pop in reverse order that you push, since a stack follows a "first in, last out" structure

True

LODS

loads from memory if operand is of 1 byte, it's loaded into AL register if 1 word, AX register if 1 doubleword, EAX register

What's the proper syntax for exiting a function?

mov esp, ebp pop ebp ret (first 2 lines could be replaced with "leave")


Related study sets

Earth/Environmental Science - Released Form

View Set

Biology 1001 Chapter15 Thinking Critically

View Set

Fluid, Electrolyte & Acid-base Balance PrepU N400

View Set

ets practice test incorrect and unsure Q's

View Set

Macroeconomic Chapter 7, 8 , 10, 11 Q&A

View Set

Chapter Exam Group Life Insurance

View Set