Вы находитесь на странице: 1из 10

ITESM CEM Grisel Yarinda Morales Ortiz - A01163133 Santiago Len Ortiz - A01168632 Assignment 3 Q2-1 What is the

difference between a big-endian and little-endian data representation? In Little-endian mode the most least byte is stored in the highest bits of the word and in Bigendian mode the most significant byte is stored in the low-order bits of the word. Q2-2 What is the difference between the Harvard and von Neumann architectures? Von Neumann architectures is a computer whose memory holds both data and instructions, unlike Harvard architecture that has two memories, one for data and the other for program. The data and instructions in a Von Neumann architecture can be read or written when an address is given, so the CPU fetches the instruction from memory, decodes it and finally executes it. The internal register of the CPU in charge of pointing to an instruction in memory is the Program Counter (PC). On the other hand, the PC in a Harvard architecture only points to program memory and not to data memory and it provides some perks like higher memory bandwidth because the data and memory dont compete for the same port and it makes easier to move large amounts of data at precise intervals. Q2-3 Answer the following questions about the ARM programming model: a. How many general-purpose registers are there? There are 16 general-purpose registers: r0 through r15. b. What is the purpose of the CPSR? CPSR (Current Program Status Register) is set during every arithmetic, logical, or shifting operation and the top four bits (N, Z, C, V) of this register provide useful information about the results of them, for example if it has gotten a negative result, if the result is zero, if there is a carry out of the operation or if the operation results in an overflow. c. What is the purpose of the Z bit? To notify if the operation result is zero. d. Where is the program counter kept? It is stored in the r15 register (a general-purpose register) Q2-5 Write ARM assembly code to implement the following C assignment: z=a*(b+c)-d*e; Code /* ej_5: z=a*(b+c)-d*e;*/ .data .align 2

a: b: c: d: e: z: LC1: LC2: LC3: LC4: LC5: LC6: LC7:

.word .word .word .word .word .word .asciz .align .asciz .align .asciz .align .asciz .align .asciz .align .asciz .align .asciz

0 0 0 0 0 0 "Enter the value 2 "Enter the value 2 "Enter the value 2 "Enter the value 2 "Enter the value 2 "%d" 2 "z = a*(b+c)-d*e of a: " of b: " of c: " of d: " of e: "

= %d\n"

.text .align 2 .global main main: push {lr} ldr r0, =LC1 bl printf ldr r0, =LC6 ldr r1, =a bl scanf mov r0, #'\n' bl putchar ldr r0, =LC2 bl printf ldr r0, =LC6 ldr r1, =b bl scanf mov r0, #'\n' bl putchar ldr r0, =LC3 bl printf ldr r0, =LC6 ldr r1, =c bl scanf

/* /* /* /* /* /*

r0 <- &LC1 call printf r0 <- &LC6 r1 <- &a call scanf line break

*/ */ */ */ */ */

/* /* /* /* /*

r0 <- &LC2 call printf r0 <- &LC6 r1 <- &b line break

*/ */ */ */ */

/* /* /* /* /*

r0 <- &LC3 call printf r0 <- &LC6 r1 <- &c line break

*/ */ */ */ */

mov r0, #'\n' bl putchar ldr r0, =LC4 bl printf ldr r0, =LC6 ldr r1, =d bl scanf mov r0, #'\n' bl putchar ldr r0, =LC5 bl printf ldr r0, =LC6 ldr r1, =e bl scanf mov r0, #'\n' bl putchar ldr ldr ldr ldr ldr ldr ldr ldr ldr ldr r4, r4, r2, r2, r3, r3, r5, r5, r6, r6, =a [r4] =b [r2] =c [r3] =d [r5] =e [r6] /* /* /* /* /* r0 <- &LC4 call printf r0 <- &LC6 r1 <- &d line break */ */ */ */ */

/* /* /* /* /*

r0 <- &LC5 call printf r0 <- &LC6 r1 <- &e line break

*/ */ */ */ */

/* /* /* /* /* /* /* /* /* /* /* /* /* /* /*

r4 r4 r2 r2 r3 r3 r5 r5 r6 r6

<<<<<<<<<<-

&a *a &b *b &c *c &d *d &e *e

*/ */ */ */ */ */ */ */ */ */

add r2, r2, r3 mul r0, r4, r2 mul r1, r5, r6 sub r1, r0, r1 ldr r0, =LC7 bl printf mov r0, #'\n' bl putchar mov r0, #0 pop {pc}

r2 <r0 <r1 <r0 <print

b+c */ a*(b+c) */ d*e */ a*(b+c)-d*e */ data format */

/* Return value */

Checking The data used to check the function was: a=1, b=2, c=3, d=4, e=5 Screenshot

Q2-6 What is the meaning of these ARM condition codes? a. EQ: Equals zero b. NE: Not equal to zero c. MI: Minus d. VS: Overflow e. GE: Signed greater than or equal f. LT: Signed less than Q2-9 Write ARM assembly code to implement the following C conditional:
if (xy<3) { a = bc; x = 0; } else { y = 0; d = e+f+g; }

Code push {lr} ldr r0, =LC1 bl printf ldr r0, =LC8 ldr r1, =a bl scanf mov r0, #'\n' bl putchar ldr r0, =LC2 /* /* /* /* /* /* r0 <- &LC1 call printf r0 <- &LC8 r1 <- &b call scanf line break */ */ */ */ */ */

/* r0 <- &LC2

*/

bl printf ldr r0, =LC8 ldr r1, =c bl scanf mov r0, #'\n' bl putchar ldr r0, =LC3 bl printf ldr r0, =LC8 ldr r1, =e bl scanf mov r0, #'\n' bl putchar ldr r0, =LC4 bl printf ldr r0, =LC8 ldr r1, =f bl scanf mov r0, #'\n' bl putchar ldr r0, =LC5 bl printf ldr r0, =LC8 ldr r1, =g bl scanf mov r0, #'\n' bl putchar ldr r0, =LC6 bl printf ldr r0, =LC8 ldr r1, =x bl scanf mov r0, #'\n' bl putchar ldr r0, =LC7 bl printf ldr r0, =LC8 ldr r1, =y bl scanf mov r0, #'\n' bl putchar ldr r3, =b ldr r3, [r3]

/* /* /* /*

call printf r0 <- &LC8 r1 <- &c line break

*/ */ */ */

/* /* /* /* /*

r0 <- &LC3 call printf r0 <- &LC8 r1 <- &e line break

*/ */ */ */ */

/* /* /* /* /*

r0 <- &LC4 call printf r0 <- &LC8 r1 <- &f line break

*/ */ */ */ */

/* /* /* /* /*

r0 <- &LC5 call printf r0 <- &LC8 r1 <- &g line break

*/ */ */ */ */

/* /* /* /* /*

r0 <- &LC6 call printf r0 <- &LC8 r1 <- &x line break

*/ */ */ */ */

/* /* /* /* /*

r0 <- &LC7 call printf r0 <- &LC8 r1 <- &y line break

*/ */ */ */ */

/* r3 <- &b */ /* r3 <- *b */

ldr ldr ldr ldr ldr ldr ldr ldr ldr ldr ldr ldr

r4, =c r4, [r4] r6, =e r6, [r6] r7, =f r7, [r7] r8, =g r8, [r8] r9, =x r9, [r9] r10, =y r10, [r10]

/* /* /* /* /* /* /* /* /* /* /* /*

r4 <- &c */ r4 <- *c */ r6 <- &e */ r6 <- *e */ r7 <- &f */ r7 <- *f */ r8 <- &g */ r8 <- *g */ r9 <- &x */ r9 <- *x */ r10 <- &y */ r10 <- *y */

#testing condition mov r11, #3 sub r9, r9, r10 cmp r9, r11 bgt falseblock #true block sub r0, r3, r4 ldr r3, =a str r0, [r3] mov r11, #0 ldr r3, =x str r11, [r3] ldr r1, =a ldr r1, [r1] ldr r0, =LC9 bl printf mov r0, #'\n' bl putchar ldr r1, =x ldr r1, [r1] ldr r0, =LC10 bl printf mov r0, #'\n' bl putchar b end #false block falseblock: mov r11, #0 ldr r3, =y str r11, [r3] add r6, r6, r7 add r6, r6, r8 ldr r3, =d str r6, [r3] ldr r1, =y ldr r1, [r1] ldr r0, =LC11

/* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /*

r11 <- 3 */ r9 <- r9 + r10 */ comparing x-y with 3 */ if x-y > 3 branch to falseblock */ r0 <- b-c */ r3 <- &r3 */ a <- r0 */ r11 <- 0 */ r3 <- &r3 */ x <- r11 */ r1 <- &a */ r1 <- *a */ print data format */ call printf */ break line */ r1 <- &x */ r1 <- *x */ print data format */ call printf */ break line */

/* branch to end */ /* /* /* /* /* /* /* /* /* /* r11 <- 0 */ r3 <- &y */ y <- r11 */ r6 <- e + f*/ r6 <- e + f + g*/ r3 <- &d */ r6 <- *d */ r1 <- &y */ r1 <- *y */ print data format */

bl printf mov r0, #'\n' bl putchar ldr r1, =d ldr r1, [r1] ldr r0, =LC12 bl printf mov r0, #'\n' bl putchar end: mov r0, #0 pop {pc}

/* call printf */ /* line break */ /* /* /* /* /* r1 <- &d */ r1 <- *d */ print data format */ call printf */ line break */

/* Return value */

Checking The data used to check the true block was: b= 2, c= 3, d= 4, e= 5, f= 6, g= 7 x= 2, y= 1 The data used to check the false block was: b= 2, c= 3, d= 4, e= 5, f= 6, g= 7 x= 2, y= 1 Screenshot

Q2-10 Write ARM assembly language code for the following loops: for (i = 0; i < 3; i++)

for (j = 0; j< 3; i++) z[i] = a[i][j] * b[i]; for (i = 0; i < 3; i++) printf (z[%d] = %d\n, i, z[i]);
Code .data .align 2 a: .word 1 .word 2 .word 3 .word 4 .word 5 .word 6 .word 7 .word 8 .word 9 b: .word 2 .word 4 .word 6 z: .word 0 .word 1 .word 2 .word 3 LC7: .asciz

"z[%d] = %d\n"

.text .align 2 .global main main: push {lr} mov r0, #0 @set i to 0 mov r1, #0 @set j to 0 loop1: loop2: mov r5, #3 mul r2, r0, r5 /* r2 <- r0 * 3 */ add r2, r2, r1 /* r2 <- r2 + r1 */ ldr r3, =a /* r3 <- &a */ ldr r3, [r3,+r2] /* r3 <- *a */ ldr r4, =b /* r4 <- &b */ ldr r4, [r4, r0] /* r4 <- *(b+r2) */ mul r3, r4, r3 ldr r4, =z str r3, [r4, r0] add r0, r0, #1 cmp r0, #3 bne loop2 mov r0, #0 add r1, r1, #1 cmp r1, #3

bne loop1 mov r1, #0 ldr r3, =z ldr r2, [r3, #0] ldr r0, =LC7 /* print data format */ bl printf mov r1, #1 ldr r3, =z ldr r2, [r3, #1] ldr r0, =LC7 /* print data format */ bl printf mov r1, #2 ldr r3, =z ldr r2, [r3, #2] ldr r0, =LC7 /* print data format */ bl printf mov r0, #0 pop {pc} /* Return value */

Screenshot Apparently the offset to move along a is being calculated wrong, and even though we dont get segmentation fault, the output is not the correct one, but it is always the same.

Q2-11 Explain the operation of the BL instruction, including the state of ARM registers before and after its operation. BL (Branch and Link) instruction is used to properly return after a function/procedure call. It is like a branch, but BL saves the next PC value from r15 to r14 (lr), so when the procedure/function is finished, the value of r14 can be moved back to r15 (PC) in order to continue the execution of the

program before the branch.This is why its often used to call procedures or functions. As an example, suppose the BL instruction is called in the following way: BL my_tag where my_tag points to address 0xA0, and the instruction BL itself was being executed in address 0xB0. The value of PC before the execution of the instruction is 0xB0, when the line is executed PC gets the value 0xA0, and LR gets 0xB1. This way, the end of the function call defined by my_tag can be the line BX lr This will return to the execution of the code before the branch.

Вам также может понравиться