CMPT 295

Go over Rounding - Lecture 6 Slide 13:

Rounding

  1. Round up
  2. Round down
  3. Round half way -> when bits to right of rounding position are 10002\text{100\dots 0}_{2}
    • Round to even number: produces 0 as the least significant bit of rounded result.

Example: Round to nearest 1/41/4 (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
23322 \frac{3}{32} 10.000112\text{10.00011}_{2} 10.002\text{10.00}_{2} (<1/2–down) 22
23162 \frac{3}{16} 10.00.1102\text{10.00.110}_{2} 10.012\text{10.01}_{2} (>1/2–up) 2142 \frac{1}{4}
2782 \frac{7}{8} 10.111002\text{10.11100}_{2} 11.002\text{11.00}_{2} (1/2–up to even)) 33
2582 \frac{5}{8} 10.101002\text{10.10100}_{2} 10.102\text{10.10}_{2} (1/2–down to even) 2122 \frac{1}{2}

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: 1100 1100 1100 1100 1100 11zero0 11001100 \space 1100 \space 1100 \space 1100 \space 1100 \space 11\text{zero}0 \space \overline{1100}

Assignment 3 Question 1 a. iv

frac: 0101 0101 0101 0101 0101 01zero 1010101 \space 0101 \space 0101 \space 0101 \space 0101 \space 01\text{zero} \space \overline{101}

Assignment#2 Question 2 g., h., i., k.

Exponent Fraction Value
Description Bit Representation/th> exp E 2E2^{E} frac M M2EM 2^{E} V Decimal
zero 0 000 00 0 -2 1/41/4 0/40/4 0/40/4 0/160/16 0 0.0
Smallest positive denormalized 0 000 01 0 -2 1/41/4 1/41/4 1/41/4 1/161/16 1/161/16 0.0625
0 000 10 0 -2 1/41/4 2/4=1/22/4=1/2 2/4=1/22/4=1/2 2/162/16 2/162/16 0.125
Largest positive denormalized 0 000 11 0 -2 1/41/4 3/43/4 3/43/4 3/163/16 3/163/16 0.1875
Smallest positive normalized 0 001 00 1 -2 1/41/4 0/40/4 4/4=14/4=1 4/164/16 4/164/16 0.25
0 001 01 1 -2 1/41/4 1/41/4 5/45/4 5/165/16 5/165/16 0.3125
0 001 10 1 -2 1/41/4 2/4=1/22/4=1/2 6/46/4 6/166/16 6/166/16 0.375
0 001 11 1 -2 1/41/4 3/43/4 7/47/4 7/167/16 7/167/16 0.4375
0 010 00 2 -1 1/21/2 0/40/4 4/4=14/4=1 4/84/8 4/84/8 0.5
0 010 01 2 -1 1/21/2 1/41/4 5/45/4 5/85/8 5/85/8 0.625
0 010 11 2 -1 1/21/2 3/43/4 7/47/4 7/87/8 7/87/8 0.875
One 0 011 00 3 0 1 0/40/4 4/4=14/4=1 4/44/4 4/44/4 1.0
0 011 01 3 0 1 1/41/4 5/45/4 5/45/4 5/45/4 1.25
0 011 10 3 0 1 2/4=1/22/4=1/2 6/46/4 6/46/4 6/46/4 1.5
0 011 11 3 0 1 3/43/4 7/47/4 7/47/4 7/47/4 1.75
0 100 00 4 1 2 0/40/4 4/4=14/4=1 8/48/4 8/48/4 2
0 100 01 4 1 2 1/41/4 5/45/4 10/410/4 10/410/4 2.5
0 100 10 4 1 2 2/4=1/22/4=1/2 6/46/4 12/412/4 12/412/4 3
0 100 11 4 1 2 3/43/4 7/47/4 14/414/4 14/414/4 3.5
0 101 00 5 2 4 0/40/4 4/4=14/4=1 16/416/4 16/416/4 4
0 101 01 5 2 4 1/41/4 5/45/4 20/420/4 20/420/4 5
0 101 10 5 2 4 2/4=1/22/4=1/2 6/46/4 24/424/4 24/424/4 6
0 101 11 5 2 4 3/43/4 7/47/4 28/428/4 28/428/4 7
0 110 00 6 3 8 0/40/4 4/4=14/4=1 32/432/4 32/432/4 8
0 110 01 6 3 8 1/41/4 5/45/4 40/440/4 40/440/4 10
0 110 10 6 3 8 2/4=1/22/4=1/2 6/46/4 48/448/4 48/448/4 12
Largest positive normalized 0 110 11 6 3 8 3/43/4 7/47/4 56/456/4 56/456/4 14
+ infinity 0 111 0 - - - - - - \infty -
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 [14.014.0][-14.0\dots 14.0] not considering ±\pm\infty and NaN (since it’s not a contiguous range)

h. What is the range of the normalized exponent E (E found in the equation v=(-1)sM2Ev=\text{(-1)}^{s} M 2^{E}) which can be represented by this 6-bit floating representation?

Range of E[25]E \rightarrow [-2\dots 5] is the same as exp[001110]\text{exp} \rightarrow [001\dots 110]

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 ±\pm\infty, 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.

  1. the 5th letter of the Greek alphabet — see Alphabet Table. 2: an arbitrarily small positive quantity in mathematical analysis.

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: 1M<2ϵ1\leq M \lt 2 - \epsilon? Express E as a fractional decimal number.

First, let’s fix the above equation 1M<2ϵ1 \leq M < 2 - \epsilon. It should be 1M<21 \leq M < 2.

Remember:

answer:

The value of “frac” of the largest denormalized number is .11=3/4=0.7510\text{.11} = 3/4 = \text{0.75}_{10}

How close is the value of the “frac” of the largest normalized number to 1=1/4=0.25101 = 1/4 = \text{0.25}_{10}

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