Chapter 3 - Machine-Level Representation of Programs Flashcards

1
Q

What register is used to store where a computer is in its program sequence?

A

The program counter register, %rip.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which register is associated with the program counter?

A

%rip.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many locations are there in a typical modern integer register file?

A

16 registers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many bits of information can each register in the register file hold?

A

64 bits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What program can be used on a Linux system to view the assembly code of a given object file?

A

objdump.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What term is given to the class of programs used to inspect the contents of machine-code files?

A

Disassemblers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What command can be used to generate a file containing the assembly-level version of the source file prog.cc?

A

g++ -Og -S prog.cc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using g++, a source file is translated into both assembly code (with the -S flag) and an executable (with the -o flag). What is likely to be the most noticeable difference after dumping the contents of the generated files? (Either using ‘objdump’ or ‘cat’.)

A

The offset in the address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the Intel format differ from the ATT format?

A
  1. Intel code omits size designation suffixes (i.e. push not pushq).
  2. Intel code omits the ‘%’ character in front of register names.
  3. Intel code describes memory locations differently (e.g. QWORD PTR [rbx] rather than (%rbx)).
  4. Intel code lists operands in reverse order to ATT code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
State the number of bytes taken to represent the following data types: 
char
short
int
long
char*
float
double
A
char -- 1
short -- 2
int -- 4
long -- 8
char* -- 8
float -- 4
double -- 8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many registers did the original 8086 processor have and what size data could they contain?

A

There were eight 16-bit registers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many registers did the IA32 processor have and what size data could they contain?

A

There were eight 32-bit registers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many registers does the x86-64 processor have and what size data can it contain?

A

There are 16 64-bit registers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What labels did the registers in the original 8086 processor have?

A

There were eight registers labelled %ax through to %sp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What labels did the registers in the IA32 processor have?

A

There were eight registers labelled %eax through to %esp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What labels do the registers of the x86-64 processor have?

A

There are 16 registers; the first eight registers are labelled %rax through to %rsp, and the other eight are labelled %r8 through %r15.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When instructions have registers for destinations, what happens to the remaining bytes when the instructions generate size 1, 2 or 4-byte values?

A

When 1 and 2-byte values are generated, the remaining bytes in the register are left untouched.

When 4-byte values are generated, the high-order 4 bytes are set to zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which register is associated with the stack pointer?

A

%rsp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What suffixes are used in integer instructions to indicate the size of the operand?

A
1-byte = 'b'
2-byte = 'w'
4-byte = 'dw'
8-byte = 'qw'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the syntax for providing an immediate value as an operand to an assembly instruction?

A

The given value is preceded by the $ character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the general syntax for providing a memory location as an operand to an assembly instruction?

A

The syntax has the form Imm(rb,ri,s).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

An assembly instruction is given “Imm(rb,ri,s)” as an operand. Describe what this operand represents.

A

Imm(rb,ri,s) corresponds to the byte at the memory location:
M[ Imm + R[rb] + R[ri] * s ]

The individual terms are:
Imm = Immediate offset
R[rb] = Base register
R[ri] = Index register
s = scaling index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the three main types of operands supplied to assembly instructions?

A

Immediate, register and memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Assume the following values are stored at the indicated memory addresses and registers:

Address Value 
0x100 0xFF 
0x104 0xAB
0x108 0x13
0x10C 0x11

Register Value
%rax 0x100
%rcx 0x1
%rdx 0x3

What are the values of the following operands?
260(%rcx,%rdx)
0xFC(,%rcx,4)
(%rax,%rdx,4)

A

260(%rcx,%rdx) = M[0x108] = 0x13.

0xFC(,%rcx,4) = M[0x104] = 0xFF.

(%rax,%rdx,4) = M[0x10C] = 0x11.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Name the four instructions in the MOV class.

A

movb, movw, movl, and movq.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

True or false? Both operands supplied to a move instruction can be memory locations.

A

False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How many instructions does it take to copy data from one memory location to another?

A

Two; one to copy the data into a register and another to copy it to the destination.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the movabsq instruction used for and what operand can it have for the destination?

A

To move 64-bit immediate values, and the destination must be a register.

N.B. movq can only handle 32-bit immediate values which are sign-extended to 64 bits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What two classes of move instructions can be used to copy a smaller source to a larger destination?

A

The movz and movs class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Name the five different movz instructions.

A

movzbw, movzbl, movzwl, movzbq, and movzwq.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Why is there no movzlq instruction?

A

A 4-byte source is automatically zero extended when copied it to a 8-byte destination, so the movl instruction automatically implements the movzlq instruction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Name the six different movs instructions.

A

movsbw, movsbl, movswl, movsbq, movswq, and movslq.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What instruction requires no operands and sign extends a 4-byte source to a 8-byte location? What register does it operate on?

A

The cltq instruction, which operates on the %rax register.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

For each of the following lines of assembly language, determine the appropriate instruction suffix based on the operands.
mov___ %eax, (%rsp)
mov___ (%rax), %dx
mov___ $0xFF, %bl

A

movl %eax, (%rsp)
movw (%rax), %dx
movb $0xFF, %bl

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

For each of the following lines of assembly language, determine the appropriate instruction suffix based on the operands.
mov___ (%rsp,%rdx,4), %dl
mov___ (%rdx), %rax
mov___ %dx, (%rax)

A

movb (%rsp,%rdx,4), %dl
movq (%rdx), %rax
movw %dx, (%rax)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Each of the following lines of code generates an error message when we invoke the assembler. Explain what is wrong with each line.
movb $0xF, (%ebx)
movl %rax, (%rsp)
movw (%rax),4(%rsp)

A

movb $0xF, (%ebx)
// Memory references require 8-byte registers.
movl %rax, (%rsp)
// Mismatch between instruction suffix and register ID
movw (%rax),4(%rsp)
// Only one operand can be a memory location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Each of the following lines of code generates an error message when we invoke the assembler. Explain what is wrong with each line.
movb %al,%sl
movq %rax,$0x123

A

movb %al,%sl
// There is no register named sl.
movq %rax,$0x123
// The destination cannot be an immediate value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Each of the following lines of code generates an error message when we invoke the assembler. Explain what is wrong with each line.
movl %eax,%rdx
movb %si, 8(%rbp)

A

movl %eax,%rdx
// The destination of the movl instruction cannot be an 8-byte register.
movb %si, 8(%rbp)
// Mismatch between instruction suffix and register ID.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Which register is associated with an integer return value?

A

%rax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Assume variables sp and dp are declared with types

src_t *sp;
dest_t *dp;

where src_t and dest_t are data types declared with typedef. We wish to use the appropriate pair of data movement instructions to implement the operation

*dp = (dest_t) *sp;

Assume that the values of sp and dp are stored in registers %rdi and %rsi. For each entry below, show the two instructions that implement the specified data movement.

S = long, D = long:
movq (%rdi), %rax
movq %rax, (%rsi)

S = char, D = int:
______________
______________

S = char, D = unsigned:
______________
______________

A

S = char, D = int:
movsbl (%rdi), %eax
movl %eax, (%rsi)

S = char, D = unsigned:
movsbl (%rdi), %eax
movl %eax, (%rsi)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Assume variables sp and dp are declared with types

src_t *sp;
dest_t *dp;

where src_t and dest_t are data types declared with typedef. We wish to use the appropriate pair of data movement instructions to implement the operation

*dp = (dest_t) *sp;

Assume that the values of sp and dp are stored in registers %rdi and %rsi. For each entry below, show the two instructions that implement the specified data movement.

S = unsigned char, D = long:
______________
______________

S = int, D = char:
______________
______________

A

S = unsigned char, D = long:
movzbl (%rdi), %eax
movq %rax, (%rsi)
// Note the special trick with the first step; this relies on the fact that the high-order 4 bytes of the register will be cleared with this instruction.

S = int, D = char:
movl (%rdi), %eax
movb %al, (%rsi)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Assume variables sp and dp are declared with types

src_t *sp;
dest_t *dp;

where src_t and dest_t are data types declared with typedef. We wish to use the appropriate pair of data movement instructions to implement the operation

*dp = (dest_t) *sp;

Assume that the values of sp and dp are stored in registers %rdi and %rsi. For each entry below, show the two instructions that implement the specified data movement.

S = unsigned, D = unsigned char:
______________
______________

S = char, D = short:
______________
______________

A

S = unsigned, D = unsigned char:
movl (%rdi), %eax
movb %al, (%rsi)

S = char, D = short:
movsbw (%rdi), %ax
movw %ax, (%rsi)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What discipline does a stack adhere to?

A

A “last-in, first-out” (LIFO) discipline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What effect does the pushq instruction have?

A

The stack pointer is decremented by 8 (to allocate 8 bytes) and the quad word provided as an operand is written to the value at the new top-of-stack address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What effect does the popq instruction have?

A

The quad word is read from the top-of-stack location and stored at the operand destination, then the stack pointer is incremented by 8 (to deallocate 8 bytes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What has the smallest address, the top of the stack or the bottom?

A

The top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What has the smallest address, the top of the stack or the bottom?

A

The top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What does the cltq instruction do? How many operands does it take?

A

It sign extends a 4-byte source to a 8-byte location and requires no operands; it sign extends the data in %eax and stores it at %rax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Which assembly instruction is used to generate pointers?

A

The leaq instruction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What instruction classes are used to compute x++, x–, -x, and ~x?

A
x++ = INC
x-- = DEC
-x = NEG
~x = NOT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

How many operands do instructions in the INC, DEC, NEG, and NOT classes take?

A

The instructions in these classes apply unary operations. They take a single operand which corresponds to both the source and the destination.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

How many operands do instructions in the ADD, SUB, MUL, DIV, XOR, OR and AND classes take?

A

They take two operands: a source, and a destination (in that order).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What are the classes of instructions used for left and right-shift operations? Are they all unique?

A

SAL, SAR, SHL, SHR. The SAL and SHL are not unique; they both have the same effect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is the result of the following assembly code?

long scale(long x, long y, long z)
x in %rdi, y in %rsi, z in %rdx
scale:
leaq (%rdi,%rsi,4), %rax
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,4), %rax
ret
A

long t = x + 4y + 12z;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the result of the following assembly code?

short scale3(short x, short y, short z)
x in %rdi, y in %rsi, z in %rdx
scale3:
leaq (%rsi,%rsi,9), %rbx
leaq (%rbx,%rdx), %rbx
leaq (%rbx,%rdi,%rsi), %rbx
ret
A

long t = 10y + z + xy;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Suppose register %rbx holds value p and %rdx holds value q. Fill in the table below with formulas indicating the value that will be stored in register %rax for each of the given assembly-code instructions:

leaq 9(%rdx), %rax   
\_\_\_\_\_\_\_\_\_\_\_\_
leaq (%rdx,%rbx), %rax   
\_\_\_\_\_\_\_\_\_\_\_\_
leaq (%rdx,%rbx,3), %rax   
\_\_\_\_\_\_\_\_\_\_\_\_
A
leaq 9(%rdx), %rax          = 9+q
leaq (%rdx,%rbx), %rax    = p+q
leaq (%rdx,%rbx,3), %rax = q+3*p
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

Suppose register %rbx holds value p and %rdx holds value q. Fill in the table below with formulas indicating the value that will be stored in register %rax for each of the given assembly-code instructions:

leaq 2(%rbx,%rbx,7), %rax
\_\_\_\_\_\_\_\_\_\_\_\_
leaq 0xE(,%rdx,3), %rax
\_\_\_\_\_\_\_\_\_\_\_\_
leaq 6(%rbx,%rdx,7), %rax
\_\_\_\_\_\_\_\_\_\_\_\_
A
leaq 2(%rbx,%rbx,7), %rax  = 2+8*p
leaq 0xE(,%rdx,3), %rax     = 14+3*q
leaq 6(%rbx,%rdx,7), %rax  = 6+p+7*q
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What type of operands can be supplied to a bit-shift instruction?

A

An immediate value or a single-byte register.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is the shift amount of the following instruction?

shll $0xF3,%eax

A

A 32-bit value can only be shifted by 2^5 so only the first five bits of 0xF3 are used, i.e. [10011]. Thus, the shift amount is 19.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What does the cqto instruction do?

A

Convert a quad word to an oct word.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

What is the difference between the imulq and mulq instructions?

A
imulq = signed multiplication
mulq = unsigned multiplication
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

If the imulq and mulq instructions are only provided one operand, what is their function?

A

They are special instructions that provide full 128-bit multiplication and division.

imulq = signed multiplication
mulq = unsigned multiplication
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

Name four of the most useful condition flags.

A

The carry flag, zero flag, sign flag, and overflow flag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the carry flag used for?

A

It indicates that the most recent operation generated a carry out of the most significant bit. It’s used to detect overflow of unsigned calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

What is the zero flag used for?

A

To indicate that the most recent operation yielded zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is the sign flag used for?

A

To indicate that the most recent operation yielded a negative value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

What is the overflow flag used for?

A

To indicate that the most recent operation caused a two’s complement overflow – either positive or negative.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

What two classes of instructions set the condition codes without altering any registers?

A

The CMP and TEST classes of instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

What is the syntax of an instruction in the CMP family and what is its effect?

A

Syntax: cmp{b,w,l,q} S_1, S_2.
Effect: Set condition flags based on S_2 - S_1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is the syntax of an instruction in the TEST family and what is it’s effect?

A

Syntax: test{b,w,l,q} S_1, S_2.
Effect: Set condition flags based on S_2 & S_1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

The condition flags are CF, ZF, SF, and OF. Combine them to produce the instruction “setle”.

A
(SF ^ OF) | ZF
// Note: this form ensures that the most recent operation has not overflowed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

The condition flags are CF, ZF, SF, and OF. Combine them to produce the instruction “setg”.

A
~(SF ^ OF) & ~ZF
// Note: this form ensures that the most recent operation has not overflowed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

The C code:

int comp(data_t a, data_t b) {
return a COMP b;
}

shows a general comparison between arguments a and b, where data_t, the data type of the arguments, is defined (via typedef) to be an integer data types and either signed or unsigned. The comparison COMP is defined via #define.

Suppose a is in some portion of %rdx while b is in some portion of %rsi. For each of the following instruction sequences, determine which data types data_t and which comparisons COMP could cause the compiler to generate this code.

A. cmpl %esi, %edi
setl %al

B. cmpw %si, %di
setge %al

A

A. cmpl %esi, %edi
setl %al
// Data types: int
// Operation: a<b>=b</b>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

The C code:

int comp(data_t a, data_t b) {
return a COMP b;
}

shows a general comparison between arguments a and b, where data_t, the data type of the arguments, is defined (via typedef) to be an integer data types and either signed or unsigned. The comparison COMP is defined via #define.

Suppose a is in some portion of %rdx while b is in some portion of %rsi. For each of the following instruction sequences, determine which data types data_t and which comparisons COMP could cause the compiler to generate this code.

A. cmpb %sil, %dil
setbe %al

B. cmpq %rsi, %rdi
setne %a

A

A. cmpb %sil, %dil
setbe %al
// Data types: unsigned char
// Operation: a<=b

B. cmpq %rsi, %rdi
setne %a
// Data types: long, unsigned long or a pointer
// Operation: a!=b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

What three common ways are there to access condition codes?

A

(1) We can set a single byte to 0 or 1 depending on some combination of the condition codes;
(2) We can conditionally jump to some other part of the program, or;
(3) We can conditionally transfer data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

What do the suffixes of the instructions in the SET class denote?

A

The condition, NOT the data size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

The C code:

int test(data_t a) {
return a TEST 0;
}

shows a general comparison between argument a and 0, where we can set the data type of the argument by declaring data_t with a typedef, and the nature of the comparison by declaring TEST with a #define declaration. The following instruction sequences implement the comparison, where a is held in some portion of register %rdi. For each sequence, determine which data types data_t and which comparisons TEST could cause the compiler to generate this code.

A. testq %rdi, %rdi
setge %al

B. testw %di, %di
sete %al

A

A. testq %rdi, %rdi
setge %al
// Data types: long
// Operation: a>=0

B. testw %di, %di
sete %al
// Data types: short
// Operation: a==0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

The C code:

int test(data_t a) {
return a TEST 0;
}

shows a general comparison between argument a and 0, where we can set the data type of the argument by declaring data_t with a typedef, and the nature of the comparison by declaring TEST with a #define declaration. The following instruction sequences implement the comparison, where a is held in some portion of register %rdi. For each sequence, determine which data types data_t and which comparisons TEST could cause the compiler to generate this code.

A. testb %dil, %dil
seta %al

B. testl %edi, %edi
setle %al

A

A. testb %dil, %dil
seta %al
// Data types: unsigned char
// Operation: a>0

B. testl %edi, %edi
setle %al
// Data types: int
// Operation: a<=0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

What instruction can cause the execution to switch to a completely new position in the program?

A

The jmp instruction.

80
Q

In assembly, what is used to indicate the jump destination?

A

A label.

81
Q

What is the term given to the addresses of a jump instruction destination?

A

Jump target.

82
Q

What two types of unconditional jump instruction are there?

A

A direct and indirect jump.

83
Q

What is the syntax for an indirect jump?

A

jmp *operand.

The operand is either a register or memory location.

84
Q

What two types of address encoding can be used for jump instructions?

A

PC relative and with an “absolute” address.

85
Q

What is the target of the je instruction below? (You do not need to know anything about the callq instruction here.)

4003fa: 74 02 je XXXXXX
4003fc: ff d0 callq *%rax

A

4003fa: 74 02 je XXXXXX
4003fc: ff d0 callq *%rax
// 0x2 is the offset from the byte after the jump instruction, 0x4003fc. Thus, the jump target is 0x4003fe.

86
Q

What is the target of the je instruction below?

40042f: 74 f4 je XXXXXX
400431: 5d pop %rbp

A

40042f: 74 f4 je XXXXXX
400431: 5d pop %rbp
// The jump is 0xf4=-12. Adding this to 0x400431 gives the jump target 0x400425.

87
Q

What is the address of the ja and pop instructions?
XXXXXX: 77 02 ja 400547
XXXXXX: 5d pop %rbp

A
XXXXXX: 77 02     ja 400547
XXXXXX: 5d          pop %rbp
// The jump target, 0x400547, is the address of the byte after the jump instruction plus 0x2. Subtracting 0x2 from 0x400547 gives 0x400545 so the filled in assembly code is:
400543: 77 02     ja 400547
400545: 5d          pop %rbp
88
Q

In the code that follows, the jump target is encoded in PC-relative form as a 4-byte two’s-complement number. The bytes are listed from least significant to most, reflecting the little-endian byte ordering of x86-64. What is the address of the jump target?

4005e8: e9 73 ff ff ff jmpq XXXXXXX
4005ed: 90 nop

A

4005e8: e9 73 ff ff ff jmpq XXXXXXX
4005ed: 90 nop
// The target of the jump is encoded as 0xffffff73 (note that this is on a little endian machine). Negating and adding one gives 0x8D=261, so the jump is -0x8D=-261. Therefore, the jump target is 400560.

89
Q

Describe the two ways to implement a conditional transfer of control.

A

Option 1:

t = test-expr;
if (!t)
goto false;
then-statement
goto done;
false:
else-statement
done:

Option 2:

t = test-expr;
if (t)
goto true;
else-statement
goto done;
true:
then-statement
done:
90
Q

You are told about the two types of conditional transfer of control for an if-statement (given below). Which option is better, and why?

Option 1:

t = test-expr;
if (!t)
goto false;
then-statement
goto done;
false:
else-statement
done:

Option 2:

t = test-expr;
if (t)
goto true;
else-statement
goto done;
true:
then-statement
done:
A

Option 1 is better because it is easier to adapt when there is no “else” statement. It is also has a similar structure to the normal form of an if statement.

91
Q

When given the C code:

void cond(short a, short *p)
{
if (a &amp;&amp; *p < a)
*p = a;
}

gcc generates the following assembly code:

void cond(short a, short *p)
a in %rdi, p in %rsi
cond:
testq %rdi, %rdi
je .L1
cmpq %rsi, (%rdi)
jle .L1
movq %rdi, (%rsi)
.L1:
rep; ret

Explain why the assembly code contains two conditional branches, even though the C code has only one if statement.

A

The first conditional branch is part of the implementation of the && expression. If the test for a being non-null fails, the code will skip the test of a >= *p.

92
Q

What is the benefit of PC relative addressing?

A

It allows object code to be relocatable, i.e. it can be shifted to different portions of memory without alteration.

93
Q

What are the two different ways to implement conditional branches?

A

Conditional transfer of control and conditional transfer of data.

94
Q

When is conditional transfer of data a viable option?

A

When the amount of data that needs to be computed is small.

95
Q

What does conditional transfer of data involve?

A

This approach computes both outcomes of a conditional operation and then selects one based on whether or not the condition holds.

96
Q

Fill in the blank:
A conditional transfer of data involves computing both outcomes of a conditional operation and then selecting one based on whether or not the condition holds. This strategy makes sense only in restricted cases, but it can then be implemented by a simple ___________ _______ instruction.

A

A conditional transfer of data involves computing both outcomes of a conditional operation and then selecting one based on whether or not the condition holds. This strategy makes sense only in restricted cases, but it can then be implemented by a simple conditional move instruction.

97
Q

What instruction class is key to a conditional transfer of data?

A

The CMOV instruction class.

98
Q

Describe the typical form of a conditional transfer of data.

A

v = then-expr;
ve = else-expr;
t = test-expr;
if (!t) v = ve;

99
Q

What are the two conditional move instructions for when the result of the most recent operation is equal to zero or not equal to zero?

A

cmove and cmovne.

100
Q

What are the two conditional move instructions for when the result of the most recent operation is negative or not negative?

A

cmovs and cmovns.

101
Q

Describe the typical form of an instruction from the CMOV class.

A

cmov[X] S,D, which moves the data from the source to the destination D when the condition specified by [X] holds.

102
Q

What size registers can be used for conditional move instructions?

A

2, 4 or 8 byte registers.

103
Q

Give two examples of when conditional data transfers won’t be used by the compiler.

A

For example, when the result of one statement has a side effect or can generate an error condition.

104
Q

In the following C function, we have left the definition of operation OP incomplete:

#define OP /* Unknown operator */
short arith(short x) {
return x OP 16;
}

When compiled, gcc generates the following assembly code:

short arith(short x)
x in %rdi
arith:
leaq 15(%rdi), %rbx
testq %rdi, %rdi
cmovns %rdi, %rbx
sarq $4, %rbx
ret

What operation is OP?

A

OP is the divide operator, implemented with the right shift “»4”

105
Q

What do processors use to guess whether or not each jump instruction will be followed that allow them to achieve high performance?

A

Branch prediction logic.

106
Q

State the assembly form of a do-while loop.

A
loop:
body-statement
t = test-expr;
if (t)
goto loop;
107
Q

In what two ways are while loops implemented?

A

As “jump in the middle” and “guarded do” while loops.

108
Q

State the general form of a “jump in the middle” while loop.

A
goto test;
loop:
body-statement
test:
t=test-expr;
if (t) 
goto loop;
109
Q

State the general form of a “guarded do” while loop.

A
t=test-expr;
if (!t)
goto done;
loop:
body-statement
t=test-expr;
if (t)
goto loop;
done:
110
Q

The gcc C compiler generates the following assembly code:

short test_one(unsigned short x)
x in %rdi
 test_one:
 movl $1, %eax
 jmp .L5
 .L6:
 xorq %rdi, %rax
 shrq %rdi Shift right by 1
 .L5:
 testq %rdi, %rdi
 jne .L6
 andl $0, %eax
 ret

What loop translation method was used?

A

A “jump in the middle” while loop.

111
Q

Describe the general form of a for loop based on a “guarded-do” while loop.

A
init-expr;
t=test-expr;
if (!t)
goto done;
loop:
body-statement
update-expr;
t=test-expr;
if (t)
goto loop;
done:
112
Q

When might we encounter a problem if we naively applied our rule for translating our for loop into a while loop?

A

When the body statement contains continue or break statement.

(The update-expr expression would not be evaluated when the continue statement is satisfied resulting in infinite recursion.)

113
Q

Fill in the blank:
A switch statement provides a multiway branching capability based on the value of an integer index. They are particularly useful when dealing with tests where there can be a large number of possible outcomes. Not only do they make the C code more readable, but they also allow an efficient implementation using a data structure called a _____ _____.

A

A switch statement provides a multiway branching capability based on the value of an integer index. They are particularly useful when dealing with tests where there can be a large number of possible outcomes. Not only do they make the C code more readable, but they also allow an efficient implementation using a data structure called a jump table.

114
Q

What is a jump table?

A

A jump table is an array where entry i is the address of a code segment implementing the action the program should take when the switch index equals i.

115
Q

When might a jump table be used in a switch statement?

A

When the test cases span a small range of values and there are a number of cases.

116
Q

What is a “computed goto”?

A

A goto location determined using a jump table (seen in switch statements).

117
Q

Optional: complete Practice Problem 3.31 on page 274.

A

Optional: complete Practice Problem 3.31 on page 274.

118
Q

Fill in the blank:
When an x86-64 procedure requires storage beyond what it can hold in registers, it allocates space on the stack. This region is referred to as the procedure’s ______ ______.

A

When an x86-64 procedure requires storage beyond what it can hold in registers, it allocates space on the stack. This region is referred to as the procedure’s stack frame.

119
Q

What is placed at the top of a function’s stack frame when it calls another function?

A

The return address.

120
Q

Function P calls function Q. Which functions’s stack frame does the return address of P go on?

A

P’s stack frame.

121
Q

How many arguments are typically passed on the stack?

A

If there are six or less arguments then they may all be passed through registers.

If there are more than six arguments, the remaining arguments are store on the stack.

122
Q

What assembly instruction is used to invoke a procedure?

A

The call instruction.

123
Q

What two registers are always affected by a function call?

A

%rsp, the stack pointer register, and %rip, the program counter register.

124
Q

Name the six registers used to pass function arguments in the order that they are used.

A

%rdi, %rsi, %rdx, %rcx, %r8, and %r9.

125
Q

List three common cases in which local variables must be stored on the stack.

A
  1. When there are not enough registers to hold the local data.
  2. When the memory address of a variable is required.
  3. When some of the local variables are arrays or struct. (Access of these variables require array or structure references.)
126
Q

What is a “callee-saved” register?

A

A register whose value must be preserved by the callee.

127
Q

What is a “caller-saved” register?

A

A register whose value must be preserved by the caller.

128
Q

Which registers are classified as “callee-saved”?

A

%rbx, %rbp, and %r12-%r15.

129
Q

Which registers are classified as “caller-saved”?

A

All registers except for %rbx, %rbp, %r12-%r15 and %rsp.

130
Q

How can a callee function preserve the value of a callee-saved register?

A

By not overwriting it or saving its value on the stack and popping it before returning.

131
Q

Suppose that during the execution of a function P, one value is stored in register %rbx and one is stored in register %rdi. Function P does not save these values before calling function Q. Are these values guaranteed to be preserved?

A

No. Register %rbx is a callee-saved register so its value will be preserved by the function Q, but register %rdi is a caller-saved register so its value should be preserved by function P before invoking function Q.

132
Q

Given an array A with starting address x_A and data type size L, what is the address of element i in the array?

A

x_A + L*i.

133
Q

Suppose E is an array of values of type int and we wish to evaluate E[i], where the address of E is stored in register %rdx and i is stored in register %rcx. What instruction would be used to access this value and store it in register %eax?

A

movl (%rdx,%rcx,4), %eax.

134
Q

Suppose E is an array of values of type int and we wish to evaluate *(E+i-3). The address of E is stored in register %rdx and i is stored in register %rcx. What instruction would be used to access this value and store it in register %eax?

A

(E+i-3) corresponds to the memory reference M[x_E+4i-12], which can be evaluated using the instruction:
movl -12(%rdx,%rcx,4), %eax

135
Q

Suppose x_P, the address of short integer array P, and long integer index i are stored in registers %rdx and %rcx, respectively. The result should be stored in register %rax if it is a pointer and register element %ax if it has data type short. Fill in the table below:

Expression: P[1]
Type: _____________
Value: _____________
Assembly code: _____________

A

Expression: P[1]
Type: short
Value: M[x_P + 2]
Assembly code: movw 2(%rdx), %ax

136
Q

Suppose x_P, the address of short integer array P, and long integer index i are stored in registers %rdx and %rcx, respectively. The result should be stored in register %rax if it is a pointer and register element %ax if it has data type short. Fill in the table below:

Expression: P[2]
Type: _____________
Value: _____________
Assembly code: _____________

A

Expression: P[2]
Type: short
Value: M[x_P + 4]
Assembly code: movw 4(%rdx), %ax

137
Q

Suppose x_P, the address of short integer array P, and long integer index i are stored in registers %rdx and %rcx, respectively. The result should be stored in register %rax if it is a pointer and register element %ax if it has data type short. Fill in the table below:

Expression: P + 3 + i
Type: _____________
Value: _____________
Assembly code: _____________

A

Expression: P + 3 + i
Type: short*
Value: x_P + 2*i + 6
Assembly code: leaq 6(%rdx,%rcx,2), %rax

138
Q

Suppose x_P, the address of short integer array P, and long integer index i are stored in registers %rdx and %rcx, respectively. The result should be stored in register %rax if it is a pointer and register element %ax if it has data type short. Fill in the table below:

Expression: P[i * 6 - 5]
Type: _____________
Value: _____________
Assembly code: _____________

A

Expression: P[i * 6 - 5]
Type: short
Value: M[x_P + 12*i - 10]
Assembly code: movw -10(%rdx,%rcx,12),%ax

139
Q

Suppose x_P, the address of short integer array P, and long integer index i are stored in registers %rdx and %rcx, respectively. The result should be stored in register %rax if it is a pointer and register element %ax if it has data type short. Fill in the table below:

Expression: &P[i + 2]
Type: _____________
Value: _____________
Assembly code: _____________

A

Expression: &P[i + 2]
Type: short*
Value: x_P + 2*i + 4
Assembly code: leaq 4(%rdx,%rcx,2), %rax

140
Q

Given an array declared as
T D[R][C];
what is the general form for the memory address of array element D[i][j]?

A

&D[i][j]=x_D + L(C*i+j).

141
Q

Consider the following source code, where M and N are constants declared with #define:

long P[M][N];
long Q[N][M];
long sum_element(long i, long j) {
     return P[i][j] + Q[j][i];
}

In compiling this program, gcc generates the following assembly code:

long sum_element(long i, long j)
i in %rdi, j in %rsi
sum_element:
     leaq 0(,%rdi,8), %rdx
     subq %rdi, %rdx
     addq %rsi, %rdx
     leaq (%rsi,%rsi,4), %rax
     addq %rax, %rdi
     movq Q(,%rdi,8), %rax
     addq P(,%rdx,8), %rax
     ret

Determine the values of M and N based on this assembly code.

A

We can see that the reference to matrix P is at byte offset 8(7i + j), while the reference to matrix Q is at byte offset 8(5j + i). From this, we can determine that P has 7 columns, while Q has 5, giving M=5 and N=7.

142
Q

What two mechanisms are provided by C to create data types by combining objects of different types?

A

Structures and unions.

143
Q

What size does a struct have?

A

The sum of the sizes of each contained field, plus the padding needed to satisfy the alignment requirement of each field.

144
Q

Consider the following structure declaration:

struct rec {
     int i;
     int j;
     int a[2];
     int *p;
};

Suppose variable r is of type “struct rec*” and is in register %rdi.

What five instructions would be used to implement the following statement?

r->p = &r->a[r->i + r->j];

A

Command: r->p = &r->a[r->i + r->j];
Register: r in %rdi.

Instructions:
movl 4(%rdi), %eax // Get r->j
addl (%rdi), %eax // Add r->i
cltq // Extend to 8 bytes
leaq 8(%rdi,%rax,4), %rax // Compute &r->a[r->i + r->j]
movq %rax, 16(%rdi) // Store in r->p

145
Q

Consider the following structure declaration:

struct test {
     short *p;
     struct {
        short x;
        short y;
     } s;
     struct test *next;
};

What are the offsets (in bytes) of the following fields?

p: ____________
s. x: ____________
s. y: ____________
next: ____________

A

p: 0
s. x: 8
s. y: 10
next: 12

146
Q

Consider the following structure declaration:

struct test {
     short *p;
     struct {
        short x;
        short y;
     } s;
     struct test *next;
};

The compiler generates the following assembly code for st_init:

void st_init(struct test *st)
st in %rdi
st_init:
     movl 8(%rdi), %eax
     movl %eax, 10(%rdi)
     leaq 10(%rdi), %rax
     movq %rax, (%rdi)
     movq %rdi, 12(%rdi)
     ret

to implement the code:

void st_init(struct test *st) {
st->s.y = \_\_\_\_\_\_\_\_\_;
st->p = \_\_\_\_\_\_\_\_\_;
st->next = \_\_\_\_\_\_\_\_\_;
}

Fill in the blanks.

A
void st_init(struct test *st) {
st->s.y = st->s.x;
st->p = st->s.y;
st->next = st;
}
147
Q

The following code shows the declaration of a structure of type ACE and the prototype for a function test:

struct ACE {
short v;
struct ACE *p;
};

short test(struct ACE *ptr);

When the code for fun is compiled, gcc generates the following assembly code:

short test(struct ACE *ptr)
ptr in %rdi
test:
     movl $1, %eax
     jmp .L2
.L3:
     imulq (%rdi), %rax
     movq 2(%rdi), %rdi
.L2:
     testq %rdi, %rdi
     jne .L3
     rep; ret

A. Use your reverse engineering skills to write C code for test.
B. Describe the data structure that this structure implements and the operation performed by test.

A
A. 
short test(struct ACE *ptr)
{
short result=1;
while (ptr!=0) {result*=ptr->v; ptr=ptr->p;}
return result;
}

B. The data structure is a singly-linked list and the function computes the product of all the values in the list.

148
Q

What size does a union have?

A

The size of the largest field, plus whatever padding is needed to satisfy its alignment requirement.

149
Q

Name a benefit of a union.

A

It can be used to access the bit pattern of a data type.

150
Q

Suppose we write a procedure that will create an 8-byte double using the bit patterns given by two 4-byte unsigned values, word0 and word1:

double uu2double(unsigned word0, unsigned word1)
{
union {
double d;
unsigned u[2];
} temp;

temp.u[0] = ________;
temp.u[1] = ________;
return temp.d;
}

Fill in the blanks using the arguments word0 and word1.

A

On a little endian machine, the least significant bytes (corresponding to word0) will be stored first and the most significant bytes (corresponding to word1) will be stored last. On such a machine, the code is given by:

double uu2double(unsigned word0, unsigned word1)
{
union {
double d;
unsigned u[2];
} temp;

temp.u[0] = word0;
temp.u[1] = word1;
return temp.d;
}

On a big endian machine, word0 and word1 are the other way around.

151
Q

What are “alignment restrictions”?

A

The restriction (placed by many computer systems) on the allowable address for the primitive data types, requiring that the address for some objects must be a multiple of some value K (typically 2, 4, or 8).

152
Q

What alignment restriction does a K-byte data type have?

A

Its address must be a multiple of K.

153
Q

What size does the following struct have?

struct S1 {
int i;
char c;
int j;
};
A

12 bytes. This includes the 3 bytes needed to pad the character c so that j meets its 4-byte alignment restriction.

154
Q

What size does the following struct have?

struct S2 {
int i;
int j;
char c;
};
A

12 bytes. This includes the 3 bytes needed to pad the end of that structure so that it meets a 4-byte alignment restriction necessary to allocate an array of structs of type S2.

155
Q

For each of the following structure declarations, determine the offset of each field, the total size of the structure, and its alignment requirement for x86-64:

A. struct P1 { short i; int c; int *j; short *d; };
B. struct P2 { int i[2]; char c[8]; short s[4]; long *j; };

A
A. struct P1 { short i; int c; int *j; short *d; };
// Offset (in bytes): i=0, c=4, j=8, d=16.
// Total size: 24 bytes.
// Alignment requirement: 8.
B. struct P2 { int i[2]; char c[8]; short s[4]; long *j; };
// Offset (in bytes): i=0, c=8, s=16, j=24.
// Total size: 32 bytes.
// Alignment requirement: 8.
156
Q

For each of the following structure declarations, determine the offset of each field, the total size of the structure, and its alignment requirement for x86-64:

A. struct P3 { long w[2]; int *c[2] };
B. struct P4 { char w[16]; char *c[2] };

A
A. struct P3 { long w[2]; int *c[2] };
// Offset (in bytes): w=0, c=16.
// Total size: 32 bytes.
// Alignment requirement: 8.
B. struct P4 { char w[16]; char *c[2] };
// Offset (in bytes): w=0, c=16.
// Total size: 32 bytes.
// Alignment requirement: 8.
157
Q

You are given the structure declarations:

struct P1 { short i; int c; int *j; short *d; };
struct P4 { char w[16]; char *c[2] };

For the following structure declaration, determine the offset of each field, the total size of the structure, and its alignment requirement for x86-64:

A. struct P5 { struct P4 a[2]; struct P1 t };

A
A. struct P5 { struct P4 a[2]; struct P1 t };
// Offset (in bytes): a=0, t=64.
// Total size: 88 bytes.
// Alignment requirement: 3 bytes to pad short i.
158
Q

What is “buffer overflow”?

A

A common source of state corruption resulting from an out-of-bounds access.

159
Q

Optional: complete practice problem 3.46.

A

Optional: complete practice problem 3.46.

160
Q

What is the term given to code which is used to maliciously take advantage of buffer overflow vulnerabilities?

A

Exploit code.

161
Q

Name three techniques used to thwart buffer overflow attacks.

A

Stack randomisation (subset of ASLR; address-space layout randomisation), stack protection, and limiting the regions of memory that can hold executable code.

162
Q

What is “stack randomisation”?

A

The randomisation of the position of the stack used to help thwart buffer overflow attacks.

163
Q

What is “stack protection”?

A

Stack protection refers to the usage of a “canary” value on the stack to detect whether a buffer has overflowed.

164
Q

Name a common (brute force) trick that an attacker can use to overcome stack randomisation and create a buffer overflow.

A

Adding a “nop sled” (consisting of a long sequence of “nop”s) before the actual exploit code.

This increases the chances of the overwritten return address jumping onto the exploit code.

165
Q

Where is a stack protector (or a guard value) typically placed?

A

Between any local buffer and the rest of the stack state.

166
Q

How does a stack protector work?

A

It is placed between any local buffer and the rest of the stack state once storage on the stack is allocated. Before restoring the register state and returning from the function, the program checks if the guard value has been altered. If so, the program aborts with an error.

167
Q

How is a variable-size stack frame managed?

A

Using the frame (or base) pointer, %rbp.

168
Q

What is the frame pointer and what is it’s purpose?

A

Register %rbp serves as a frame pointer, and is used to manage variable size stack frames.

169
Q

What instruction is used before “ret” to restore the base pointer and the stack pointer? What two instructions is it equivalent to?

A

The “leave” instruction.

It is equivalent to the instructions:
movq %rbp, %rsp
// Set the stack pointer to the beginning of the frame
popq %rbp
// Restore the saved value of %rbp (a callee-saved register) and set the stack pointer to the end of caller’s frame.

170
Q

With x86-64 code, when is the frame pointer used?

A

When the stack frame may be of variable size.

171
Q

Optional: complete practice problem 3.49.

A

Optional: complete practice problem 3.49.

172
Q

Early media instructions used to support graphics and image processing and originally focused on allowing multiple operations to be performed in parallel. What is the name of this parallel mode?

A

Single-instruction, multiple-data (SIMD) mode.

173
Q

In 1997, both Intel and AMD introduced a SIMD mode with the MMX extension to the x86 architecture. Name two further revisions of the MMX extension. What do the initials of each stand for?

A

SSE (streaming SIMD extensions) and AVX (advanced vector extensions).

174
Q

How many bits/bytes can an XMM register hold?

A

128 bits/16 bytes.

175
Q

How many bits/bytes can a YMM register hold?

A

256 bits/32 bytes.

176
Q

Which media registers are dedicated to storing procedure arguments?

A

Registers %ymm0-%ymm7.

177
Q

Which media registers are caller-saved?

A

Registers %ymm8-%ymm15.

178
Q

State the instructions used to unconditionally move data between XMM registers and memory (not register to register).

A

vmovss (single-precision) and vmovsd (double-precision)

179
Q

State the instructions used to unconditionally move data between XMM registers (not to or from memory).

A

vmovaps and vmovapd.

180
Q

State the four instructions used to convert a floating-point (float/double) number to an integer (int/long) and the possible source/destinations.

A

vcvttss2si, vcvttsd2si, vcvttss2siq, and vcvttsd2siq.

The source can either be a memory location or XMM register and the destination must be an integer register.

181
Q

State the four instructions used to convert an integer (int/long) to a floating-point (float/double) number and the possible source/destinations.

A

vcvtsi2ss, vcvtsi2sd, vcvtsi2ssq, and vcvtsi2sdq.

The first source can be an integer register or a memory location but the second source must be an XMM register. The destination must be an XMM register.

182
Q

What two instructions are used to convert a float to a double?

A

vunpcklps %xmm0, %xmm0, %xmm0
// Interleave values in sources and store in destination.
vcvtps2pd %xmm0, %xmm0
// Convert two vector elements to a double.

183
Q

What two instructions are used to convert a double to a float?

A

vmovddup %xmm0, %xmm0
// Replicate first vector element.
vcvtpd2psx %xmm0, %xmm0
// Convert two vector elements to single.

184
Q

The vunpcklps instruction is supplied two distinct source registers. The first contains the words [s3, s2, s1, s0] and the other contains words [d3, d2, d1, d0]. What will be the result of this instruction?

A

[s1, d1, s0, d0].

185
Q

For the following C code, the expressions val1-val4 all map to the program values i, f, d, and l:

double fcvt2(int *ip, float *fp, double *dp, long l)
{
int i = *ip; float f = *fp; double d = *dp;
*ip = (int) val1;
*fp = (float) val2;
*dp = (double) val3;
return (double) val4;
}

Determine the mapping, based on the following x86-64 code for the function:

// double fcvt2(int *ip, float *fp, double *dp, long l)
// ip in %rdi, fp in %rsi, dp in %rdx, l in %rcx
// Result returned in %xmm0
fcvt2:
movl (%rdi), %eax
vmovss (%rsi), %xmm0
vcvttsd2si (%rdx), %r8d
movl %r8d, (%rdi)
vcvtsi2ss %eax, %xmm1, %xmm1
vmovss %xmm1, (%rsi)
vcvtsi2sdq %rcx, %xmm1, %xmm1
vmovsd %xmm1, (%rdx)
vunpcklps %xmm0, %xmm0, %xmm0
vcvtps2pd %xmm0, %xmm0
ret
A
val1 = d.
val2 = i.
val3 = l.
val4 = f.
186
Q

The following C function converts an argument of type src_t to a return value of type dst_t, where these two types are defined using typedef:

dest_t cvt(src_t x)
{
dest_t y = (dest_t) x;
return y;
}

For execution on x86-64, assume that argument x is either in %xmm0 or in the appropriately named portion of register %rdi (i.e., %rdi or %edi). One or two instructions are to be used to perform the type conversion and to copy the value to the appropriately named portion of register %rax (integer result) or %xmm0 (floating-point result). Show the instruction(s), including the source and
destination registers.

src_t: long
dest_t: double
Instruction(s): vcvtsi2sdq %rdi, %xmm0

src_t: double
dest_t: int
Instruction(s): ________________

src_t: double
dest_t: float
Instruction(s): ________________

A

src_t: double
dest_t: int
Instruction(s): vcvttsd2sdi %xmm0, %eax

src_t: double
dest_t: float 
Instruction(s): 
vmovddup %xmm0, %xmm0
vcvtpd2psx %xmm0, %xmm0
187
Q

The following C function converts an argument of type src_t to a return value of type dst_t, where these two types are defined using typedef:

dest_t cvt(src_t x)
{
dest_t y = (dest_t) x;
return y;
}

For execution on x86-64, assume that argument x is either in %xmm0 or in the appropriately named portion of register %rdi (i.e., %rdi or %edi). One or two instructions are to be used to perform the type conversion and to copy the value to the appropriately named portion of register %rax (integer result) or %xmm0 (floating-point result). Show the instruction(s), including the source and
destination registers.

src_t: long
dest_t: float
Instruction(s): ________________

src_t: float
dest_t: long
Instruction(s): ________________

A

src_t: long
dest_t: float
Instruction(s): vcvtsi2ssq %rdi, %xmm0, %xmm0

src_t: float
dest_t: long
Instruction(s): vcvttss2siq %xmm0, %rax

188
Q

State the seven floating-point arithmetic operations (for float or double).

A
Add: vaddss or vaddsd
Subtract: vsubss or vsubsd
Multiply: vmulss or vmulsd
Divide: vdivss or vdivsd
Max: vmaxss or vmaxsd
Min: vminss or vminsd
Square root: sqrtss or sqrtsd
189
Q

For the following C function, the types of the four arguments are defined by typedef:

double funct1(arg1_t p, arg2_t q, arg3_t r, arg4_t s)
{
return p/(q+r) - s;
}

When compiled, gcc generates the following code:

// double funct1(arg1_t p, arg2_t q, arg3_t r, arg4_t s)
funct1:
vcvtsi2ssq %rsi, %xmm2, %xmm2
vaddss %xmm0, %xmm2, %xmm0
vcvtsi2ss %edi, %xmm2, %xmm2
vdivss %xmm0, %xmm2, %xmm0
vunpcklps %xmm0, %xmm0, %xmm0
vcvtps2pd %xmm0, %xmm0
vsubsd %xmm1, %xmm0, %xmm0
ret

Determine the possible combinations of types of the four arguments (there may be more than one).

A
double funct1a(int p, float q, long r, double s);
or
double funct1b(int p, long q, float r, double s);
190
Q

Function funct2 has the following prototype:

double funct2(double w, int x, float y, long z);

Gcc generates the following code for the function:

// double funct2(double w, int x, float y, long z)
// w in %xmm0, x in %edi, y in %xmm1, z in %rsi
funct2:
vcvtsi2ss %edi, %xmm2, %xmm2
vmulss %xmm1, %xmm2, %xmm1
vunpcklps %xmm1, %xmm1, %xmm1
vcvtps2pd %xmm1, %xmm2
vcvtsi2sdq %rsi, %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vsubsd %xmm0, %xmm2, %xmm0
ret

Write a C version of funct2.

A
double funct2(double w, int x, float y, long z)
{
return x*y - w/z;
}
191
Q

State two instructions that apply Boolean operations on an XMM register.

A

vxorps, vxorpd, vandps and vandpd.

192
Q

What two instructions does AVX2 provide for comparing floating-point values?

A

vcomiss and vcomisd.

193
Q

When is the parity flag set for integer operations?

A

When the least significant byte of the most recent operation contains an even number of bytes.

194
Q

When is the parity flag set for floating-point operations?

A

When the most recent operation yielded NaN.

195
Q

What are the values of the condition codes (with floating-point operations) when the “unordered case” arises?

A

The carry flag, zero flag and parity flag are all set to true.

196
Q

What does the “jp” instruction stand for and when is it used?

A

“Jump on parity”. It is used to jump when the result of the most recent floating-point operation yielded NaN.