Sie sind auf Seite 1von 6

CS 3420 HW 1

Kevin Gao
Kg349
Edward Suh
1. Assembly Programming
a. __main
MOV R3, #0
CMP R3, R2
BEQ End
PUSH {R4}
B Loop
Loop
LDR R4, [R0]
LSL R4, R4, #2
STR R4, [R1]
ADD R0, #4
ADD R1, #4
CMP R3, R2
BLT Loop
POP {R4}
B End
End
END
b. The code uses :
(5 * 2 bytes) + ( 8 * 2 bytes) = 26 bytes of space
c. _main
BL multby4
B End
multby4
PUSH {R4}
MOV R3, #0
CMP R3, R2
BEQ Exit
B Loop
Exit
BX LR
Loop
LDR R4, [R0]
LSL R4, R4, #2
STR R4, [R1]
ADD R0, #4
ADD R1, #4
CMP R3, R2
BLT Loop
POP {R4}
BX LR
End
END
d. R15/PC is used as RN, so the value stored to R0 is simply the result of performing
the AND operation on 0X0F and the value of the address in
PC plus 8

2. Calling Convention and Stack


a. R15, R14, R13, R0-R3, R0-R3
b. This is what the stack looks like after line 7 of the code

...

10

1024

128

When the function finishes execution, the following registers will contain these values:
(assume that i5 and i6 got popped and placed into R1 and R2

Register Value

R0 17

R1 10

R2 5
R3 4

R4 2

R5 128

R6 1024

2 c.
A double word can be passed into a function by creating multiple references. A double word is
8 bytes, or 64 bits, so it is possible for a program to be able to split the argument into two
arguments, perform the instruction encoded in the first 4 bytes, and then perform the
instruction encoded in the next 4 bytes.

3. C Programming
a. The assignment int f_n_1 = 1 is missing a semicolon. C programming requires that
assignments end with semicolons, or else it will throw an error at this line.

Assuming that the program does compile (it doesnt in its current state), the function would not
return the desired value. Because of the given loop conditions, the function will actually return
the next fibonacci number (one ahead) instead of the desired one. i.e. it will give you F(n+1)
instead of F(n)

3 b.
mylib.h ---------
#ifndef MYLIB
#define MYLIB
int fib (int n);

#endif

main.c -------
#include mylib.h
int main () {
int result;

result = fib(40);

return 0;
}
3 c.
First we convert the decimal value to hexadecimal:
102334155 (base 10) = 06197ECB (base 16). Below is the big-endian representation of
the value sent by the network computer.

Hypothetical Address Byte

0x01 06

0x02 19

0x03 7E

0x04 CB

When the local computer tries to reconstruct the number based on the data sent to it by the
network computer, it will actually build the value incorrectly because it stores values using the
little-endian representation:
CB7E1906 (base 16) = 3414038790 (base 10)

Thus, the local computer believes that it is receiving the value 3414038790 as fib(40)

3 d.
int big_to_little_endian_converter (int b) {
// b is the big endian version int l;
// Our little endian version int i;
// Loop variable
// Allows us to index the bytes of b as an array
char * bytes_of_b = (char *)&b;
// Allows us to index the bytes of l as an array
char * bytes_of_l = (char *)&l;
for (i = 0; i < 4; ++i) {
bytes_of_l[i] = bytes_of_b[3-i];
}
return l;
}
4. C-to-Assembly Translation
bsearch implements a binary search function. The integers i and j represent the upper and
lower bounds of the array. This function will check if the bounds are the same, because then it
knows to stop the search and check if the value in the last cell matches the value we are
searching for. If it does, bsearch returns the index of the cell, if not it returns a (-1) to indicate
that it couldnt find the value.
All other times when the length of the array is > 1, the function will determine the median value
of the array, and then compare that value with the value were searching for. If the median is
the target value, we return the index of the cell containing the median. If not, we check two
other cases by creating two halves of the array: the upper half of the array, aka the values
larger than the median, or the lower half, aka the values smaller than the median. If our target
value is larger than the median, then we pass in the upper half-array into the next iteration of
bsearch, set the new bounds, and keep searching on the new array. If our target value is smaller
than the current media, then we would use the lower half-array for the next iteration. We keep
doing this until either we hit the target value, or the function returns -1.

Assumptions:
R0 will get the return value
R1 contains the address to the zeroth cell in the array
R2 contains the address to the last cell in the array
R3 will hold the target value, i.e., the value being searched for.

_main
PUSH {R4, R5, R6}
MOV R6, #2
BL Bsearch
B End
Bsearch
CMP R1, R2
BEQ Return
ADD R4, R1, R2
SDIV R4, R4, R6 ; R4 is h
LDR R5, [R4]
CMP R5, R3
BEQ Return_loop
BGT Loop_gt
ADD R4, #1
MOV R1, R4
B Bsearch
Return
LDR R5, [R1]
CMP R5, R3
BEQ Return2
MVN R0, #1
POP {R6, R5, R4}
BX LR
Return2
MOV R0, R1
POP {R6, R5, R4}
BX LR
Return_loop
MOV R0, R4
POP {R6, R5, R4}
BX LR
Loop_gt
MOV R2, R4
B Bsearch
End
END

Das könnte Ihnen auch gefallen