Imagine the xmm2 register is subdivided into 4 double words. Show the gdb command that will output the double word in position 2 in hex.
p/x $xmm2.v4_int32[2]
Convert -983 to a 16-bit twos-complement 16-bit hex integer. Beware of the negative sign. Show that you know how the algorithm works.
0xFC29
Who invented (first created) the Linux kernel?
Linus Torvalds, at that time a resident of Helsinki, Finland, and now a resident of Oregon, US
Suppose edx holds a 32-bit integer, either positive or negative. Show how to expand that integer to r15 and continue to hold an integer with the same numeric value as the original number in edx.
Solution #1:
movsx r15, edx
The ‘s’ means sign
The ‘x’ means extend
Solution #2:
movsxd r15, edx
The ‘s’ means sign
The ‘x’ means extend
The ‘d’ means ‘double word’ in the source register
Solution #3
mov eax, edx
cdqe
mov r15,rax
cdqe is the key to understanding this extension. cdqe has no arguments.
It operates only on eax by expanding it to rax.
All of the above are considered to be correct answers to this question.
In the professor’s experience, Solution #1 is more common.
Another example of solution 1: movsx rdi, ax <== sign extend a 16-bit int to a 64-bit integer.
Here is an array in the segment .data: prices dq 3.5, 7.22, 4.11, 6.55, 9.13, 0.45
Use an assembly instruction to place the memory address of 6.55 into rdi.
lea rdi, [prices+3*8] or lea rdi, [prices+24]
What GDB command could have created the following output?
0x7fffffffdc50: 0xde68 0xffff 0x7fff 0x0000 0xc4de 0xf78a 0x0001 0x0000
0x7fffffffdc60: 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
0x7fffffffdc70: 0x1000 0x0002 0x0000 0x0000 0x7900 0x1bb4 0xc40b 0x9eb4
0x7fffffffdc80: 0x0000 0x0000 0x0000 0x0000 0x11ff 0x0000 0x0000 0x0000
0x7fffffffdc90: 0x11ff 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
0x7fffffffdca0: 0xde78 0xffff 0x7fff
x/43xh 0x7fffffffdc50
Convert this IEEE float number 0x3FAC 9899 0000 0000 to an equivalent decimal float number.
111001001100010011001 x 2^(-25)
Two people together invented the protocols for data transfer between two computers. One inventor received widespread praise for the accomplishment, while the other person put in a lot of hard work to make the link work but received little acknowledgment.
What are the names of these two people? First, give the name of the acknowledged inventor, and then the name of the hard worker.
(1) Vinton Cerf and (2) Bob Kahn
Circa 1972, both worked as a team. Vinton was highly praised for leading the effort to network computers, but Bob was not entirely overlooked; he received several significant awards, too.
Let r8 be the number of Pepsi bottles on an advancing conveyor belt that packs the bottles into cartons for shipment to retail stores. Each carton holds r12 number of bottles. Make a block of clear, concise assembly that will compute the number of filled cases that will be produced (r13) and the number of left-over bottles (r15) insufficient to fill a full case.
Make a block of assembly source statements that will compute 13 and r15.
Let’s state clearly the role of each register.
r8 = Total # of bottles needing to be packed
r12 = capacity of a carton by counting bottles
r13 = number of filled cases
r15 = number of bottles left over without a case
; Block that computes the number of full cases needed to pack a shipload of bottles
mov rax, r8
cqo
div r12
mov r13, rax
mov r15, rdx
; End of block
Suppose a register had the capacity of n number of bits. Give answers in decimal digits.
a) What is the largest twos-complement number that can be placed in this register?
b) What is the smallest twos-complement integer number that can be placed in this register?
a) 2^(n-1)-1
b) -(2^(n-1))
Declare char message [ ] = “Santa Claus has arrived.” Find a gdb command that will output the truncated string “Santa Claus”.
p/s {char[11]}message or p/s *message@12 or printf “%.11s”,message”
Begin with this decimal float number 121.6. Convert it to an IEEE 64-bit hex float number. Show your intermediate steps, so I know you understand the conversion algorithm.
0x405E 6666 6666 6666
Who initiated the study and appreciation of computer algorithms?
Donald Knuth
In the California desert, there is a huge recycling center where used aluminum cans are stored until the aluminum smelter is ready to receive a shipment. Cans are shipped in trucks carrying about 1,850 pounds per load. The recycling center estimates that several million pounds of aluminum are awaiting shipment to the smelter.
Here is the problem. Create a block of assembly instructions that will compute how many truckloads will be required to haul all the cans away and how many pounds will be left over as less than a full truckload. All numbers are integers.
Given r10 = number of pounds of cargo in one truck
r12 = estimate of total pounds of cans to be hauled away
To be computed: r14 = number of trucks with full loads needed
r15 = number of pounds of left-over cans insufficient to fill one truck.
; Block that computes two numbers: quotient and remainder
mov rax, r12
cqo
div r10
mov r14, rax
mov r15, rdx
Make a macro that employs three parameters. The first two parameters are xmm registers holding the lengths of two sides of a right triangle. The third parameter is an output parameter containing the length of the hypotenuse of the right triangle.
Recall that the length of the hypotenuse is the square root of the sum of the squares of the two legs of the triangle.
%macro hypotenuse 3
movsd xmm8, %1
movsd xmm9, %2
; Square the two sides
mulsd xmm8, xmm8
mulsd xmm9, xmm9
; Add the two squares
addsd xmm8, xmm9
; Take the square root to get the hypotenuse
sqrtsd xmm10, xmm9
movsd %3, xmm10
%endmacro
What is the gdb command that will show the first 22 dwords in hex, beginning at the top of the stack? [Caution: this is about dwords, not qwords].
x/22xw $rsp
The float number 4.0 is very nice. It has a neighbor on the lower side and on the upper side. Sometimes, 4.0 wants to reach out to its higher neighbor and become friends. How far away is its higher neighbor? Show your mathematical work in the space below. Make sure the grader can find your distance answer.
S means “starting point.”
N means “neighbor.”
S = 4.0 = 100.0 x 2^0 = 1.0 x 2^2. Now we know the following:
Hidden bit = 1
Mantissa = 0
True exp = 2
Stored exponent = 3FF + 2 = 401
S = 4.0 = 0x4010 0000 0000 0000 = 1.0 x 2^2
Think about the nearest larger neighbor N = 0x4010 0000 0000 0001
N = 1.000000 ….0001 x 2^2
- S = -1.000000 0000 x 2^2
D = 0.000000…. 0001 x 2^2
The difference D is the distance from 4.0 to its nearest high-side neighbor.
D = 0.000000….0001 x 2^2
= 1.0 x 2^(-52) x 2^2
= 1.0 x 2^(-50)
= 2^(-50) <== That’s the distance (fully simplified)
Show (a block of) assembly instructions that will change bit number 20 in rcx to its opposite value.
There are three known possible correct answers.
First answer (common):
Declare an integer with 1 in position 20 and zeros everywhere else.
mask equ 0x0000 0000 0010 0000
Then perform “or”
or rcx, mask
Second valid answer (also common):
xor rcx, 0100000
Third valid answer:
xor rcx, (1<<20)
Any one of the answers above is valid for full credit.
This array of char has been declared in the main driver.
char greeting [ ] = “Happy Anniversary”;
What is the gdb command that will output “Anniversary”?
greeting is a pointer to the memory address where ‘H’ is stored. We need to output the chars starting at ‘A’ and continuing to the first null. We need the address of character ‘A’. We know that greeting is the address of ‘H’, and therefore, greeting+6 is the address of ‘A’.
Conclusion:
Correct answer #1 is p/s greeting+6
Correct answer #2 is x/s greeting+6
Correct answer #3 is x/s &greeting[6]
What is the distance between 2.0 and the closest higher neighbor of 2.0?
2^(-51)
There is a website where new members are asked to enter their profile data into a form. One question is “Enter birth year or enter Ctrl+d for decline to state”.
There is a variable declared in the .bss segment like this: birthyear resq 1. If the user inputs a positive integer that it is placed in birthyear. If the user inputs a control-d, then -99 is placed in birthyear. [There is no input validation in this discussion.]
Create an assembly block that will place the correct value in birthyear.
segment .data
intform db “%ld”, 0
segment .bss
birthyear resq 1
; Block that will input the age of the user into the variable birthyear
mov rax, 0
mov rdi, intform
mov rsi, birthyear
call scanf
cmp eax, -1
jne continue
mov [birthyear], -99
continue:
; End of block
Comment: This block exhibits a legitimate use of the 32-bit register eax. scanf uses the 32-bit register, and we also use it.