Professional Documents
Culture Documents
Solution HW02
Solution HW02
2.1 For the following C statement, write the corresponding LEGv8 assembly code. Assume
that the C variables f, g, and h, have already been placed in registers X0, X1, and X2 respectively.
Use a minimal number of LEGv8 assembly instructions.
f = g + (h − 5);
Answer:
2.2 Write a single C statement that corresponds to the two LEGv8 assembly instructions
below.
ADD f, g, h
ADD f, i, f
Answer:
F=h+h+i
2.3 [5] <§§2.2, 2.3> For the following C statement, write the corresponding LEGv8
assembly code. Assume that the variables f, g, h, i, and j are assigned to registers
X0,X1,X2,X3, and X4, respectively. Assume that the base address of the arrays A and B are
in registers X6 and X7, respectively.
B[8] = A[i−j];
Answer:
SUB X9, X3, X4 // compute i-j
LSL X9, X9, #3 // multx8 to conv word offset to byte offset
ADD X11, X6, X9 // compute &A[i-j]
LDUR X10, [X11, #0] // load A[i-j]
STUR X10, [X7, #64] // store in B[8]
or
SUB X9, X2, X4
LSL X9, X9, #3
ADD X1, X6, X9
LDUR X10, [X11, #0]
STUR X10, [X7, #64]
2.4 [10] <§§2.2, 2.3> For the LEGv8 assembly instructions below, what is the
corresponding C statement? Assume that the variables f, g, h, I, and j are assigned to registers
X0, X1, X2, X3, and X4, respectively. Assume that the base address of the arrays A and B are
in registers X6 and X7, respectively.
LSL X9, X0, #3 // X9 = f*8
ADD X9, X6, X9 // X9 = &A[f]
LSL X10, X1, #3 // X10 = g*8
ADD X10, X7, X10 // X10 = &B[g]
LDUR X0, [X9, #0] // f = A[f]
2.6 [5] <§2.3> Show how the value 0xabcdef12 would be arranged in memory of a little-
endian and a big-endian machine. Assume the data are stored starting at address 0 and that
the word size is 4 bytes.
Answer:
big-endian
(memory)
low ---------------------> high
ab | cd | ef | 12
little-endian
(memory)
low ---------------------> high
12 | ef | cd | ab
or
2.9 [10] <§§2.2, 2.3> Translate the following LEGv8 code to C. Assume tha the variables
f, g, h, i, and j are assigned to registers X0,X1,X2,X3, and X4, respectively. Assume that the
base address of the arrays A and B are in registers X6 and X7, respectively.
ADDI X9, X6, #8
ADD X10, X6, XZR
STUR X10, [X9, #0]
LDUR X9, [X9, #0]
ADD X0, X9, X10
solution:
1. The first line computes the address of 2nd word in A (as X6 is base address of array A),
that is A[1]. This is because each word is 8 bytes long the address is store in tempory
register X9.
3. The third line stores what was in X10 into X9 ( the address of 2nd word in A. So, the c
code so far is A[1]= A.( where A is the memory address of first element in Array A)
4. The fourth line loads the Value A back into register X9.
5. The fifth line adds X9 and 10. Which are now same value and store result into X0= F. So
F = 2A.
2.10 [20] <§§2.2, 2.5> For each LEGv8 instruction in Exercise 2.9, show the value
of the opcode (Op), source register (Rn), and target register (Rd or Rt) fields. For the I-type
instructions, show the value of the immediate field, and for the R-type instructions, show the
value of the second source register (Rm).
Answer:
2.11 Assume that registers X0 and X1 hold the values 0×8000000000000000 and
0×D000000000000000, respectively.
2.11.1 [5] <§2.4> What is the value of X9 for the following assembly code?
ADD X9, X0, X1
ANSWER:
X9 = X0 + X1 = 0x8000000000000000 + 0xD000000000000000 = 0x15000000000000000
2.11.2 [5] <§2.4> Is the result in X9 the desired result, or has there been overflow?
ANSWER:
There has been overflow
2.11.3 [5] <§2.4> For the contents of registers X0 and X1 as specified above,
what is the value of X9 for the following assembly code?
SUB X9, X0, X1
ANSWER:
X9 = X0 - X1 = 0x8000000000000000 - 0xD000000000000000 = -
0x5000000000000000 (2's complement: 1011 0000 0000 0000 0000 … 0000 0000
0000)
2.11.4 [5] <§2.4> Is the result in X9 the desired result, or has there been overflow?
ANSWER:
Yes, it is our desired result.
2.11.5 [5] <§2.4> For the contents of registers X0 and X1 as specified above, what is
the value of X9 for the following assembly code?
ADD X9, X0, X1
ADD X9, X9, X0
ANSWER:
X9 = X0 + X1 = 0x8000000000000000 + 0xD000000000000000 = 0x15000000000000000
X9 = X9 + X0 = 0x15000000000000000 + 0x8000000000000000= 0x1D000000000000000
2.11.6 [5] <§2.4> Is the result in X9 the desired result, or has there been overflow?
ANSWER:
There has been overflow
2.12 Assume that X0 holds the value 128ten.
2.12.1 [5] <§2.4> For the instruction ADD X9, X0, X1, what is the range(s) of
values for X1 that would result in overflow?
ANSWER:
128ten = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
1000 0000two
The range of values forX1 is from 9223372036854775680 to 9223372036854775807.
(binary: from
1000000000000000000000000000000000000000000000000000000000000000.00000
000000 TO
111111111111111111111111111111111111111111111111111111111111111)
2.12.2 [5] <§2.4> For the instruction SUB X9, X0, X1, what is the range(s) of
values for X1 that would result in overflow?
ANSWER:
2.12.3 [5] <§2.4> For the instruction SUB X9, X1, X0, what is the range(s) of
values for X1 that would result in overflow?
ANSWER:
2.13 [5] <§§2.2, 2.5> Provide the instruction type and assembly language
instruction for the following binary value:
1000 1011 0000 0000 0000 0000 0000 0000two
Hint: Figure 2.20 may be helpful.
Answer:
ADD X0, X0, X0
2.14 [5] <§§2.2, 2.5> Provide the instruction type and hexadecimal representation
of the following instruction:
STUR X9, [X10, #32]
Answer:
2.15 [5] <§2.5> Provide the instruction type, assembly language instruction, and
binary representation of instruction described by the following LEGv8 fields:
op=0x658, Rm=13, Rn=15, Rd=17, shamt=0
Answer:
R-Formate
2.17.3 [5] <§§2.5, 2.8, 2.10> How could each of the two proposed changes decrease
the size of a LEGv8 assembly program? On the other hand, how could the proposed change
increase the size of an LEGv8 assembly program?
Answer:
Not include in course
2.20 [5] <§2.6> Provide a minimal set of LEGv8 instructions that may be used to
implement the following pseudo instruction:
NOT X10, X11 // bit-wise invert
Answer:
X9=111111111111 …. …. … 11111111
EOR X10, X9, X11