CMPT 295
- Lecture 16 – Midterm 1 Review Session
Go over Rounding - Lecture 6 Slide 13:
Rounding
- Round up
- Round down
- Round half way -> when bits to right of rounding position are
- Round to even number: produces 0 as the least significant bit of rounded result.
Example: Round to nearest (2 bits right of binary point):
The third bit after the binary point is the 24th bit. Imagine the second bit as bit 23 of frac of IEEE.
Value | Binary | Rounded | Action | Rounded Value |
---|---|---|---|---|
(<1/2–down) | ||||
(>1/2–up) | ||||
(1/2–up to even)) | ||||
(1/2–down to even) |
Assignment 3 Question 1 a. iii
Transcriber’s note: the rounding bit will be highlighted by spelling out the number instaed of using the 0/1 characters
frac:
Assignment 3 Question 1 a. iv
frac:
Assignment#2 Question 2 g., h., i., k.
Exponent | Fraction | Value | |||||||
---|---|---|---|---|---|---|---|---|---|
Description | Bit Representation/th> | exp | E | frac | M | V | Decimal | ||
zero | 0 000 00 | 0 | -2 | 0 | 0.0 | ||||
Smallest positive denormalized | 0 000 01 | 0 | -2 | 0.0625 | |||||
0 000 10 | 0 | -2 | 0.125 | ||||||
Largest positive denormalized | 0 000 11 | 0 | -2 | 0.1875 | |||||
Smallest positive normalized | 0 001 00 | 1 | -2 | 0.25 | |||||
0 001 01 | 1 | -2 | 0.3125 | ||||||
0 001 10 | 1 | -2 | 0.375 | ||||||
0 001 11 | 1 | -2 | 0.4375 | ||||||
0 010 00 | 2 | -1 | 0.5 | ||||||
0 010 01 | 2 | -1 | 0.625 | ||||||
0 010 11 | 2 | -1 | 0.875 | ||||||
One | 0 011 00 | 3 | 0 | 1 | 1.0 | ||||
0 011 01 | 3 | 0 | 1 | 1.25 | |||||
0 011 10 | 3 | 0 | 1 | 1.5 | |||||
0 011 11 | 3 | 0 | 1 | 1.75 | |||||
0 100 00 | 4 | 1 | 2 | 2 | |||||
0 100 01 | 4 | 1 | 2 | 2.5 | |||||
0 100 10 | 4 | 1 | 2 | 3 | |||||
0 100 11 | 4 | 1 | 2 | 3.5 | |||||
0 101 00 | 5 | 2 | 4 | 4 | |||||
0 101 01 | 5 | 2 | 4 | 5 | |||||
0 101 10 | 5 | 2 | 4 | 6 | |||||
0 101 11 | 5 | 2 | 4 | 7 | |||||
0 110 00 | 6 | 3 | 8 | 8 | |||||
0 110 01 | 6 | 3 | 8 | 10 | |||||
0 110 10 | 6 | 3 | 8 | 12 | |||||
Largest positive normalized | 0 110 11 | 6 | 3 | 8 | 14 | ||||
+ infinity | 0 111 0 | - | - | - | - | - | - | - | |
NaN | - | - | - | - | - | - | NaN | - |
g. What is the “range” (not contiguous) of fractional decimal numbers that can be represented using this 6-bit floating-point representation?
“range” of real numbers not considering and NaN (since it’s not a contiguous range)
h. What is the range of the normalized exponent E (E found in the equation ) which can be represented by this 6-bit floating representation?
Range of is the same as
Denormalized exponent E:
exp | E | type |
---|---|---|
000 | -2 | denormalized |
001 | -2 | normalized |
010 | -1 | normalized |
011 | 0 | normalized |
100 | 1 | normlaized |
101 | 2 | normalized |
110 | 3 | normalized |
111 | , NaN | denormlaized |
i. Give an example of a fractional decimal number that can be represented using this 6-bit floating-point representaetion, but is within the “range” of representable values.
11.0 cannot be represented but it is within the range.
From lecture 6 Slide 15
What does Epsilon mean? A small positive quantity.
- the 5th letter of the Greek alphabet — see Alphabet Table. 2: an arbitrarily small positive quantity in mathematical analysis.
- exp and frac: interpreted as unsigned values.
- if
frac
= 000…0 -> M = 1.0 - if
frac
= 111…1 -> (where means a very small value)
k. How close is the value of the frac
of the largest normalized number to 1? In other words, how close is M to 2, i.e.What is E (epsilon) in this equation: ? Express E
as a fractional decimal number.
First, let’s fix the above equation . It should be .
Remember:
answer:
The value of “frac” of the largest denormalized number is
How close is the value of the “frac” of the largest normalized number to
Assignment#3 Question 1
1) [10 points] Memory addressing modes – Marked by Aditi
Assume the following values are stored at the indicated memory addresses and registers.
Memory Address | Value |
---|---|
0x230 | 0x23 |
0x234 | 0x00 |
0x235 | 0x01 |
0x23A | 0xED |
0x240 | 0xFF |
Register | Value |
---|---|
%rdi | 0x230 |
%rsi | 0x234 |
%rcx | 0x4 |
%rax | 0x1 |
Imagine the operands in the table below are the Src (source) operands for some unspecififed assembly instructions (any instruction except lea*), fill in the following table with the appropriate answers.
Note: We do not need to know what the assembly instructions are in order to fill the table.
Operand | Operand Value (expressed in hexidecimal) | Operand Form (Choices are: immediate, register or one of the 9 memory addressing modes) |
---|---|---|
%rsi | 0x234 | Register |
(%rdi) | 0x23 | Indurect memory address mode |
$0x23A |
0x23A | immedaite value |
0x240 | 0xff | Absolute memory addressing mode (this answer is preferable to “Imm” as it is more specific than “Imm” and highlights the fact that it does not require a “S” – see first row of tables below) |
10(%rdi) |
0xED |
“Base + displacement” memory addressing mode |
560(%rcx,%rax) |
0x01 |
indexed memory addressing mode |
-550(,%rdi,2) |
0xED |
Scaled indexed memory addressing mode |
0x6(%rdi,%rax,4) |
0xED |
Scaled indexed addressing mode |
Still using the first table listed above displaying the value stored at various memory addresses and registers, fill in the following tables with three different Src (source) operands for some unspecififed assembly instructions (any instructions except lea*). For each row, this operand must result in the operand Value listed and must satified the Operand Form listed.
Operand | Value | Operand Form (Choices are: immediate, registers or one of the 9 memory addressing modes) |
---|---|---|
0x234 |
0x00 |
Absolute memory addressing mode |
(%rdi,%rax,4) |
0x00 |
Scaled indexed memory address mode |
(%rdi,%rcx) |
0x00 |
Indexed memory addressing mode |
Other answers are possible!
Assignment#3 Question 2
2) [2 marks] Machine level instructions and their memory locations – Marked by Aditi
Consider a function called arith
, defined in a file called arith.c
and called from the main function found in the file called main.c
.
This function arith
, performs some arithmatic manipulation on three parameters.
Compiling main.c
and arith.c
files, we create an executable called ar
, then we execute the command:
objdump -d ar > arith.objdump
We display the partial content of arith.objdump
below. The file arith.objdump
is the disassembled version of the executable file ar
.
Your task is to fill in its missing parts, which have been underlined:
0000000000400527 <arith>:
400527: 48 8d 04 37 lea (%rdi,%rsi,1)
40052b: 48 01 d0 add %rdx,%rax
40052e: 48 8d 0c 76 lea (%rsi,%rsi,2),%rcx
400532: 48 c1 e1 04 shl $0x4,%rcx
400536: 48 8d 54 0f 04 lea 0x4(%rdi,%rcx,1),%rdx
40053b: 48 0f af c2 imul %rdx,%rax
40053f: c3 retq
Assignment#4 Question 2
In the assembly code, there are a lot more steps than in the C code, so how to match them and create the C code.
Consier the following assembly code:
# long func(long x,int n)
# x in %rdi, n in %esi, result in %rax
func:
movl %esi,%ecx
movl $1,%edx
movl $0,%eax
jmp cond
loop:
movq %rdi,%r8
addq %rdx,%r8
orq %r8,%rax
salq %c1,%rdx # shift left %rdx by content of %c1*
cond:
testq %rdx,%rdx # %rdx <- %rdx & %rdx
jne loop # jump if not zero (when %rdx & %rdx != 0)
ret # faill thru to ret (when %rdx & %rdx == 0)
Hand tracing code!
From our Lectures 14 and 15
Example pt.1 in C
// multstore is caller
// x is %rdi
// y is %rsi
// dest is %rdx
void multstore(long x, long y, long *dest) {
long t = mult2(x, y);
*dest = t;
return;
}
Example pt.2 in C:
// mult2 callee
// a is %rdi
// b is %rsi
long mult2(long a, long b) {
long s = a * b;
return s;
}
Example pt.1 in assembly:
0000000000400540 <multstore>:
400540: push %rbx # Save %rbx (1)
400541: mov %rdx,%rbx # Save dest (2)
400544: callq 400550 <mult2> # mult2(x,y) (3)
400549: mov %rax,(%rbx) # Save at Dest (7)
40054c: pop %rbx # Restore %rbx (8)
40054d: retq # Return (9)
Example pt.2 in assembly:
0000000000400550 <mult2>:
400550: mov %rdi,%rax # a (4)
400553: imul %rsi,%rax # a*b (5)
400557: retq # Return (6)
Example – Steps 1 and 2
Register/Address | M[] Stack Value | Note |
---|---|---|
ret address | return address of caller of multstore | |
(deleted) %rsp |
(new) %rbx |
(deleted) top |
(new) %rsp |
(new) top |
Registers:
Register | Value |
---|---|
%rbx |
dest (5) |
%rsp |
(deleted) 0x120 (new) 118 |
%rax |
|
%rip (PC) |
0x400 (deleted) 540 (deleted) 541 (1) (new) 544 (4) |
%rdi |
x |
%rsi |
y |
%rdx |
dest (mem. address) |
Example – Steps 3 and 4
Register/Address | M[] Stack Value | Note |
---|---|---|
ret address | ||
(deleted) %rsp |
(deleted) %rbx (new) ret. address 0x400549 (3) |
|
(2) %rsp |
top |
Registers:
Register | Value |
---|---|
%rbx |
dest |
%rsp |
(deleted) 0x118 (new) 110 |
%rax |
a |
%rip (PC) |
0x400 (deleted)544 (deleted)549(1) (deleted)550(4) (new)553 |
%rdi |
x -> a |
%rsi |
y -> b |
%rdx |
dest |
Example – Steps 5 and 6
Register/Address | M[] Stack Value | Note |
---|---|---|
ret address | ||
(5) (new) %rsp |
%rbx |
|
(deleted) %rsp |
(new) 0x400549 | top |
Registers:
Register | Value |
---|---|
%rbx |
dest |
%rsp |
(deleted) 0x110 (new) 118 |
%rax |
a*b |
%rip (PC) |
0x400549 |
%rdi |
x -> a |
%rsi |
y -> b |
%rdx |
dest |
Example – Steps 7, 8 and 9
Register/Address | M[] Stack Value | Note |
---|---|---|
ret address | ||
%rsp |
%rbx |
Top |
Registers:
Register | Value |
---|---|
%rbx |
dest |
%rsp |
0x118 |
%rax |
a*b |
%rip |
0x400549 |
%rdi |
x (a) |
%rsi |
y (b) |
%rdx |
dest |
Next next lecture
- Introduction
- C program -> assembly code -> machine level code
- Assembly language basics: data, move operation
- Memory addressing modes
- Operation leaq and Arithmetic & logical operations
- Conditional Statement – Condition Code + cmovX
- Loops
- (highlighted) Function call – Stack – Recursion
- Overview of Function Call
- Memory Layout and Stack - x86-64 instructions and registers
- Passing control
- (highlighted) Passing data – Calling Conventions
- Managing local data
- Recursion
- Array
- Buffer Overflow
- Floating-point operations