Summary Review Flashcards
How would you send the error output from your program, a.out, to a file called error.txt?
./a.out 2> error.txt
Suppose you want to output an address in hex, what is the correct placeholder to use in your print statement?
%x
Which command allows you to write information to a specified output stream?
fprintf
Suppose you declare two variables:
int x;
int y;
Now you wish to read in two integers, separated by spaces, from stdin. What command would you use?
scanf(“%d%d”,&x,&y);
According to the text, a computer’s vocabulary is defined by its
instruction set
instruction set
if R3 = 0xffff_baad_c00f_0000, what value will be in R4 after this operation:
ASR R4, R3, #4
0xffff_fbaa_dc00_f000
0xffff_fbaa_dc00_f000
Mark all that are true of a RISC architecture
includes architectures, such as the x86 architecture.
tries to make common cases fast.
will use multiple instructions to carry out complex, less frequent operations.
.
In the ARM architecture, the MOV instruction sets a register to value. This value may come from (check all that apply):
a register
an immediate field
.
The ARM instruction SUB R3, R1, R2
performs R1 - R2 and puts the result in R3
.
The ARM instruction CMP
sets bits in the CPSR
sets bits in the CPSR
Given CMP R4, R2 which condition code bits would need to be set to indicate that R4== R2
Z
Given the following C code and its corresponding Assembler, select the appropriate instruction to put in the blank spot:
i = 10; j = 1; while (i > 0){ j = j + i; i--; }
mov R2, #10 ; i/R2 = 10 mov R3, #1 ; j/R3 = 1 lp: cmp R2, #0 ; while (i > 0){ \_\_\_\_\_ ; . add R3, R3, R2 ; j/R3 = j/R3 + i/R2; sub R2, R2, #1 ; i/R2 = i/R2 - 1; b lp ; } done:
Consider the following code fragments where target has the address 0x1018 and from has the address 0x1000. What is the immediate offset that will be encoded into the branch instruction at 0x1000?
0x1000 from: b target 0x1004 sub r10, r10, r2 0x1008 lsl r10, r10, #4 0x100c and r10, r2 0x1010 lsr r10, r10, #4 0x1014 or r0, r0, r0 0x1018 target: add r0, r0, #1 0x101c str r0, [r5, #0x100]
0x00_0004
Right. The immediate is the number of instructions between the target 0x1018 and the PC of the branch +8. 0x1008.
There are 4 instruction in between.
If the OP field (bits 27:26) of an instruction is 00, then the instruction type is:
a data processing instruction
a data processing instruction
A load or store instruction can specify a base address (from a register) and
a 12-bit unsigned immediate
What does push {r0} do to the stack pointer?
Decrements sp before storing r0 onto the stack.
Decrements sp before storing r0 onto the stack.
What does pop {r0} do to the stack pointer?Increments sp after loading from the stack into r0.
Increments sp after loading from the stack into r0.
Function Prologue and Epilogue
The prologue is the set up at the start of a function, push lr, fp and any preserved registers onto the stack. The epilogue: clean up at the end of a function, pop anything you pushed onto the stack in the prologue.
pop and push
Preserved registers
Preserved registers are maintained by the person creating the program, this is NOT automatic! Preserved registers are a convention that you as the programmer must follow. What is means it that a function can assume any preserved registers (r4-r11) it uses will contain the same values even after a call to another function.
This means that if you have a function f1 that calls another function f2, when f2 is finished and returns to f1, f1 assumes all registers r4-r11 contain the same values that were stored in those registers before the call to f2. Therefore it is the job of f2 to make sure that the values that were stored in r4-r11 when f2 was called are back in those registers before it returns to f1.
lr is stored in the stack at the start of a function so we don’t lose our caller’s address during any nested function calls. Any use of BL in our function will overwrite the lr register.
Also, note that lr is a synonym to r11 (a preserved register)
Suppose we’re going to use registers r0 through r5 in a function.
What should we do in the prologue in addition to pushing fp and lr onto the stack?
Push r4 and r5 onto the stack.
Which registers should func1 be preserving (pushing and popping)?
func1: ; some code
BX lr
main: ; some code
BL func1
; some code
fp, lr and any preserved registers func1 uses
- We have the following int array arr. Values prefixed with “0x” are in hex. Values with no prefix are in decimal.
int arr[] = [10, 20, 30, 40, 50];
The base address of arr is 0x80.
What is the value of *(arr + 2)?
What is the value of (arr + 2)?
What is the value of *(arr) + 2?
What is the value of *(arr + 2)?
30
What is the value of (arr + 2)?
0x88
What is the value of *(arr) + 2?
12
CMP R3, R0 BGE one CMP R1, #0 BNE two one: ADD R2, R1, R3 SUB R2, R2, R0 two: MOV R0, R2
if( p >= m || n == 0) {
o = n + p - m;
}
struct mystery { int h; int w; char * name; }; struct mystery * t = malloc(sizeof(struct mystery)); /* Assume there is code to properly initialize the height, weight, and name fields of the struct pointed to by t */ char start = /* Fill this in */
Which of the following options would correctly place the first character of t’s name into the variable start? For example, if name was “stormfighter”, then start should be ‘s’. Select ALL that apply.
*(t->name)
t->name[0]