Wednesday, November 20, 2013

Exercise 3.1



A
B
a.
3174
0522
b.
4165
1654

Binary conversions:


A
B
a.
011 001 111 100
000 101 010 010
b.
100 001 110 101
001 110 101 100


3.1.1 What is the sum of A and B if they represent unsigned 12-bit octal numbers? The result should be written in octal.

a)
 3174 +
 0522
 3716

011 001 111 100 +
000 101 010 010
011 111  001 110
3       7      1     6

b)
4165 +
1654
6041

100 001 110 101+
001 110 101 100
110 000 100 001
 6     0      4    1

3.1.2 What is the sum of A and B if they represent signed 12-bit octal numbers stored in sign-magnitude format? The result should be written in octal.

In sign-magnitude format, the first bit represents the sign.  1 is neg, 0 is pos
a. since the leading bit is 0, the sum is identical

3174 +
0522
3716

011 001 111 100 +
000 101 010 010
011 111  001 110
3       7      1     6

b.

4165 - 100 001 110 101  leads with a 1, so is negative.

000 001 110 101
 0     1     6     5

the negative changes our sum to subtraction

1654 -
0165
1467

001 110 101 100  -
000 001 110 101
001 100 110 111
 1     4     6     7

3.1.3 Convert A into a decimal number, assuming it is unsigned.

See spreadsheet
I put up a long hand conversion of binary to decimal.
bit*(2^11) + bit*(2^10) … + bit *(2^0)
a. 1660
b. 2165



A
B
a
7040
0444
b
4365
4312

Binary Conversions


A
B
a
111 000 100 000
000 100 100 100
b
100 011 110 101
100 011 001 010


3.1.4 What is A – B if they represent unsigned 12-bit octal numbers? The result should be written in octal.

a)
7040 -
0444
6374

111 000 100 000 -
000 100 100 100
110 011 111 100

b)
4365 -
4312
0053

100 011 110 101 -
100 011 001 010
000 000 101 011

3.1.5 What is A – B if they represent signed 12-bit octal numbers stored in sign-magnitude format? The result should be written in octal.

a)
7 = 111, so its negative

011 000 100 000
 3     0      4    0

3040 +
0444
3504

3 = 011
add the sign bit back in
111 = 7
7504

011 000 100 000 +
000 100 100 100
011 101 000 100

add sign bit
111 101 000 100
 7     5     0      4

b)
4365 and 4312 both start with the bit pattern
100
therefore they are both treated as negatives

0365 -
0312
0053

add the sign bit back in
4053

000 011 110 101 -
000 011 001 010
000 000 101 011

add sign bit back in
100 000 101 011
 4     0     5     3

3.1.6 Convert A into a binary number. What makes base 8 (octal) an attractive numbering system for representing values in computers?

I converted these numbers as part of proofs in my work as I went.
As for what makes base 8 attractive:

When Computers first started to come out, Octal was ideal to represent a word.  In more recent times, win 16/32/64 bit machines, hex has taken precedence since you have 5 bit sets and 1 bit as most significant to make a 16 bit word.  However, it is still used in some instances, such as chmod in unix because it doesn’t use symbols.

Octal, since it uses 3 binary bits, 0-7 decimal, is easy to convert mentally, which makes math calculations, such as the ones above, relatively pain free.

As a side note, apparently the Yuki language employs an octal system because they count the space between fingers, instead of fingers themselves.

Sunday, October 13, 2013

Assign 2 - Exercise 2.13 ASM


Link to source file
https://docs.google.com/file/d/0B3pg-UsmVkpFT2RGcUFVUkVYRVE/edit?usp=sharing


.data

space:    .asciiz "\n"            # Print New Line

.text
__start:
#2.13.1
#a
    li $t0, 0xAAAAAAAA
    li $t1, 0x12345678
    sll $t2, $t0, 30
    sll $t2, $t2, 14
    or $t2, $t2, $t1
    move $a0, $t2
    jal printint
#b  
    li $t0, 0xF00DD00D
    li $t1, 0x11111111
    sll $t2, $t0, 30
    sll $t2, $t2, 14
    or $t2, $t2, $t1
    move $a0, $t2
    jal printint
#output
#305419896
#286331153

#2.13.2
#a
    li $t0, 0xAAAAAAAA
    li $t1, 0x12345678
    sll $t2, $t0, 4
    andi $t2, $t2, -1
    move $a0, $t2
    jal printint
#b  
    li $t0, 0xF00DD00D
    li $t1, 0x11111111
    sll $t2, $t0, 4
    andi $t2, $t2, -1
    move $a0, $t2
    jal printint
#output
#-1431655776
#14483664  

#2.13.3
    #a
    li $t0, 0xAAAAAAAA
    li $t1, 0x12345678
    srl $t2, $t0, 3        #shift right t0 by 3 store in t2
    andi $t2, $t2, 0xFFEF    #t2 += 0xFFEF
    move $a0, $t2
    jal printint
#b  
    li $t0, 0xF00DD00D
    li $t1, 0x11111111
    srl $t2, $t0, 3        #shift right t0 by 3 store in t2
    andi $t2, $t2, 0xFFEF    #t2 += 0xFFEF
    move $a0, $t2
    jal printint
#output
#21829
#47617

#2.13.4
li $t0, 0x0000A5A5
li $t1, 0x00005A5A
sll  $t2, $t0, 1        #t2 = t0*2
andi $t2, $t2, -1    #t2 = t2 & -1
move $a0, $t2
jal printint

andi $t2, $t1, 0x00F0    #t2 = t1 & 0x00F0
srl  $t2, $t2, 2        #t2 = t2 >> 2 # t2/4
move $a0, $t2
jal printint

#output
#84810
#20

#2.13.5
xor $t2, $t2, $t2 #clear register

li $t0, 0xA5A50000
li $t1, 0xA5A50000
sll  $t2, $t0, 1        #t2 = t0*2
andi $t2, $t2,-1    #t2 = t2 & -1
move $a0, $t2
jal printint

andi $t2, $t1, 0x00F0    #t2 = t1 & 0x00F0
srl  $t2, $t2, 2        #t2 = t2 >> 2 # t2/4
move $a0, $t2
jal printint
#output
#1263140864
#0

#2.13.6
xor $t2, $t2, $t2 #clear register

li $t0, 0xA5A5FFFF
li $t1, 0xA5A5FFFF
sll  $t2, $t0, 1        #t2 = t0*2
andi $t2, $t2,-1    #t2 = t2 & -1
move $a0, $t2
jal printint

andi $t2, $t1, 0x00F0    #t2 = t1 & 0x00F0
srl  $t2, $t2, 2        #t2 = t2 >> 2 # t2/4
move $a0, $t2
jal printint

#output
#1263271934
#60

    j done
       
printint:  
    addi $v0, $zero, 1        # load appropriate system call code into register $v0;
                    # code for printing integer is 1
    syscall                # call operating system to perform operation

    addi $v0, $zero, 4          # load appropriate system call code into register $v0;
                    # code for printing string is 4
    la $a0, space               # load address of the string
    syscall

    jr $ra

done:

Assign 2 - Exercise 2.13

Jumping ahead a little into something that was meant to be programmed.



In the following problems, the data table contains the values for registers $t0 and
$t1. You w be asked to perform several MIPS logical operations on these registers.
a. $t0 = 0xAAAAAAAA, $t1 = 0x12345678
b. $t0 = 0xF00DD00D, $t1 = 0x11111111

2.13.1 For the lines above, what is the value of $t2 for the following sequence of instructions?
sll $t2, $t0, 44 #left shift by t0 44 store in t2
or $t2, $t2, $t1 #t2 = t2 | t1

pseudo
t2 = (t0 << 44) || t1
a)
= (0xAAAAAAAA << 44) || 0x12345678
b)
= (0xF00DD00D << 44) || 0x11111111

This shift left operation is actually illegal, as we use 32 bit registers, so we can only shift left by 31 as a max.  However, if we adjust this to shift 30, then shift 14, we get:
a)
= (0xAAAAAAAA << 44) || 0x12345678
-0x45412988
b)
= (0xF00DD00D << 44) || 0x11111111
0x75135111
we are shifting left the entire register and then doing an or, which copies t1



2.13.2 For the values in the table above, what is the value of $t2 for the following sequence of instructions?
sll $t2, $t0, 4 #shift left t0 by 4 store in t2
andi $t2, $t2, -1 #t2 += -1

pseudo
t2 = (t0 << 4) -1
a)
= (0xAAAAAAAA << 4) -1
=0xAAAAAAA0
b)
= (0xF00DD00D << 4) -1
=0x00DD00D0
This exercise is playing with bits. Visually we are dropping the left most hex digit and sweeping in a zero, in binary, its a bit different, no pun intended.
a)
0b10101010101010101010101010101010 to
0b10101010101010101010101010100000
b)
0b11110000000011011101000000001101 to
0b00000000110111010000000011010000

2.13.3 For the lines above, what is the value of $t2 for the following sequence of instructions?
srl $t2, $t0, 3 #shift right t0 by 3 store in t2
andi $t2, $t2, 0xFFEF #t2 += 0xFFEF

pseudo
t2 = (t0 >> 3) + 0xFFEF
a)
= (0xAAAAAAAA >> 3) + 0xFFEF
=0x5545
b)
= (0xF00DD00D >> 3) + 0xFFEF
=0xBA01


In the following exercise, the data table contains various MIPS logical operations.
You will be asked to find the result of these operations given values or registers
$t0 and $t1.
a.
sll  $t2, $t0, 1 #t2 = t0*2
andi $t2, $t2, –1 #t2 = t2 & -1
b.
andi $t2, $t1, 0x00F0 #t2 = t1 & 0x00F0
srl  $t2, 2 #illegal

srl $t2, 2 is an illegal instruction as srl takes in 2 registers and an arg. I am assuming this is a typo and am changing it.

b.
andi $t2, $t1, 0x00F0 #t2 = t1 & 0x00F0
srl  $t2, $t2, 2 #t2 = t2 >> 2 # t2/4

pseudo
a. $t2 = $t0 << 1
$t2 = $t2 & -1
$t2 = ($t0 << 1) & -1

b. $t2 = $t1 & 0x00F0
$t2 = $t2 >> 2
$t2 = ($t1 & 0x00F0) >> 2

Any why we need t0 and t1 is beyond me, since they are the same value and we don’t modify it, however, I copied what was in the book anyway.

2.13.4 Assume that $t0 = 0x0000A5A5 and $t1 = 0x00005A5A. What is
the value of $t2 after the two instructions in the table?
a) = (0x0000A5A5 << 1) & -1
=0x00014b4a
b) = (0x0000A5A5 & 0x00F0) >> 2
=(0x00000050) >> 2
=0x00000014

2.13.5 Assume that $t0 = 0xA5A50000 and $t1 = 0xA5A50000.What is
the value of $t2 after the two instructions in the table?
a) = (0xA5A50000 << 1) & -1
=0x4b4a0000
b) = (0xA5A50000 & 0x00F0) >> 2
=(0x0) >> 2
=0

2.13.6 Assume that $t0 = 0xA5A5FFFF and $t1 = 0xA5A5FFFF. What is
the value of $t2 after the two instructions in the table?
a) = (0xA5A5FFFF << 1) & -1
= (0x4b4bfffe)
b) = (0xA5A5FFFF & 0x00F0) >> 2
= (0x000000f0) >> 2
=0x0000003c

In the statements above; for a) -1 is FFFFFFFF so, it will just mime the results of the first operation, where-as b actually required calculation, in most cases.



As stated in the mini intro, these problems were all about logical operations.  I am not sure if they used hex to play visual games with the results, or force students to look at what is happening at the binary level, they are called bitwise operators after all.

As stated at the very beginning, I did up a MIPS program for this exercise.  The only operands I have performed thus far was a shift left and some adds, so it was nice to touch each one of these.