วันอาทิตย์ที่ 20 กุมภาพันธ์ พ.ศ. 2554

Computer Arithmetics

Ex 2.7
a. 1010 1101 0001 0000 0000 0000 0000 0010two
b. 1111 1111 1111 1111 1011 0011 0101 0011two
2.7.1 For the patterns above, what base 10 number does it represent, assuming that it is a two’s
complement integer?
Ans 0101 0010 1110 1111 1111 1111 1111 1110
-1,391,460,350
Ans 0000 0000 0000 0000 0100 1100 1010 1101
-19,629
2.7.2 For the patterns above, what base 10 number it represent, assuming it is an unsigned integer?
1010 1101 0001 0000 0000 0000 0000 0010two
Ans 2,903,506,946
1111 1111 1111 1111 1011 0011 0101 0011two
Ans 4,294,947,667
2.7.3 For the patterns above, what hexadecimal number does it represent?
1010 1101 0001 0000 0000 0000 0000 0010two = A D 1 0 0 0 0 2
1111 1111 1111 1111 1011 0011 0101 0011two = F F F F B 3 5 3
a. 2147483647ten
b. 1000ten
2.7.4 For the base ten numbers above, convert to two’s complement binary.
a. 2147483647ten = 0111 1111 1111 1111 1111 1111 1111 1111
b. 1000ten = 0000 0000 0000 0000 0000 0011 1110 1000
2.7.5 For the base ten numbers above, convert to two’s complement hexadecimal.

a. 2147483647ten = 7 F F F F F F F
b. 1000ten = 3 E 8
2.7.6 For the base ten numbers above, convert the negated values from the table to two’s complement
hexadecimal.
a. 2147483647ten = 0111 1111 1111 1111 1111 1111 1111 1111
1000 0000 0000 0000 0000 0000 0000 0000
Ans 1000 0000 0000 0000 0000 0000 0000 0001
b. 1000ten = 0000 0000 0000 0000 0000 0011 1110 1000
1111 1111 1111 1111 1111 1100 0001 0111
Ans 1111 1111 1111 1111 1111 1100 0001 1000
-----------------------------------------------------------------------------------------------------------------
Ex.2.9
a. 2147483647ten
b. 0xD0000000sixteen
2.9.1 Assume that register $s0 = 0x70000000 and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, will there be overflow?
a. Ans : overflow
b. Ans : ไม่ overflow
2.9.2 Assume that register $s0 = 0x80000000 and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, will there be overflow?
a. Ans : ไม่ overflow
b. Ans : ไม่ overflow
2.9.3 Assume that register $s0 = 0x7FFFFFFF and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, will there be overflow?

a. Ans : ไม่ overflow
b. Ans : ไม่ overflow
a . 1010 1101 0001 0000 0000 0000 0000 0010two
b. 1111 1111 1111 1111 1011 0011 0101 0011two
2.9.4 Assume that register $s0 = 0x70000000 and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, will there be overflow?
a. Ans : ไม่ overflow
b. Ans : ไม่ overflow
2.9.5 Assume that register $s0 = 0x70000000 and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, what is the result in hex?
a. Ans : 0x1D10 0002
b. Ans : 0x6FFF B353
2.9.6 Assume that register $s0 = 0x70000000 and $s1 has the value as give in the table. If the instruction:
add $s0, $s0, $s1 is executed, what is the result in base ten?
a. Ans : 487,587,842
b. Ans : 1,879,028,563
------------------------------------------------------------------------------------------------------------------
2.10 In the following problem, the data table contains bite that represent the opcode of an instruction. You
will be asked to translate the entries into assembly code and determine what format of MIPS instruction
the bites represent
a. 1010 1110 0000 1011 0000 0000 0000 0100 two
b. 1000 1101 0000 1000 0000 0000 0100 0000two

2.10.1 For the binary entries above, what instruction do they represent?
a. Ans :
101011 10000 01011 00000 00000 000100
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
b. Ans :
100011 01000 01000 00000 00001 000000
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
2.10.2 What type (I-type, R-type) instruction do the binary entries above represent?
Ans :
a.) I-Type is as below
op rs rt constant address
6 bits 5 bits 5 bits 16 bits
101011 10000 01011 0000000000000100
R-Type is as below
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
101011 01000 01011 00000 00000 000100
b.) I-Type is as below
op rs rt constant address
6 bits 5 bits 5 bits 16 bits
100011 01000 01000 0000000001000000
R-Type is as below
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
100011 01000 01000 00000 00001 000000

2.10.3 if the binary entries above were data bites, what number would they represent in hexadecimal?
a. Ans 1010 1101 0000 1011 0000 0000 0000 0100 = A D 0 B 0 0 0 4
b. Ans 1000 1101 0000 1000 0000 0000 0100 0000 = 8 D 0 8 0 0 4 0
In the following problem, the data table contains MIPS instructions. You will be asked to translate the
retries into the bite of the opcode and determine what the MIPS instruction format is.
a. Add $to ,$to,$zero
b. Lw $t1, 4($s3)
2.10.4 For the instruction above, show the hexadecimal representation of these instruction.
Ans
Instruction Fromat op rs rt rd shamt funct
add $t0, $t0, $zero 0 8 0 8 0 20
lw $t1, 4($s3) 23 13 8 4
2.10.5[5] <2.5> What type (I-type, R-type) instruction do the binary entries above represent?
Ans
Instruction Fromat op rs rt rd shamt funct
add $t0, $t0, $zero I 000000 01000 00000 01000 0 010100
lw $t1, 4($s3) R 100011 10011 01000 0000 0000 0000 0100
2.10.5 What is the hexadecimal representation of the opcode, rs, and rt fields in this instruction?
a. Ans opcode = 0x00hex
rs = 0x08hex
rt = 0x00hex
b. Ans opcode = 0x23hex
rs = 0x13hex
rt = 0x08hex
For R-type instruction, what is the hexadecimal representation of the rd and funct fields?
Ans rd = 0x0004hex
For I-type instruction, what is the hexadecimal representation of the immediate field?
Ans immediate = 0x00hex

2.11 in the following problem, the data table contain bites that represent the opcode of an instruction. You
will be asked to translate the entries into assembly code and determine what format of MIPS instruction
the bites represent.
a. 0xAE0BFFFC
b. 0x8D08FFC0
2.11.1 What binary number does the above hexadecimal number represent?
a. Ans 0xAE0BFFFC = 1010 1110 0000 1011 1111 1111 1111 1100
b. Ans 0x8D08FFC0 = 1000 1101 0000 1000 1111 1111 1100 0000
2.11.2 What decimal number does the above hexadecimal number represent?
a. Ans = -1,374,945,344
b. Ans = -1,928,790,080
2.11.3 What instruction does the above hexadecimal number represent?
a. Ans sw $t3, 16380($s0)
b. Ans lw $t0, -64($t0)
In the following problem, the data table contains bites that represent the opcode of an instruction. You will
be asked to translate the entries into assembly code and determine what format of MIPS instruction the
bites represent.
a. Op =0,rs=1,rt=2,rd=3, shamt=0,funct=32
b. Op=0x2B,rs=0x10,,rt=0x5,const=0x4
2.11.4 What type (I-type, R-type) instruction do the instruction above represent?
a. Ans เป็น Instruction ชนิด R
b. Ans เป็น Instruction ชนิด I
2.11.5 What is the MIPS assembly instruction described above?
a. Ans
op = 0 คือ คำสัง�� overflow
rs = 1 คือ register $at
rt = 2 คือ เก็บค่าไว้ที�� $v0
rd = 3 คือ เก็บค่าไว้ที�� $v1
add $v1, $at, $v0

b. Ans
op = 0x2B คือ sw
rs = 0x10 คือ $s0 = 10
rt = 0x5 คือ $a1 = 5
const = 0x4 คือ 4
sw $a1, 4($s0)
2.11.6 What is the binary representation of the instruction above?
Ans
Instruction Fromat op rs rt rd shamt funct
add $v1, $at, $v1 I 000000 00001 00010 00011 0 100000
sw $a1, 4($s0) R 101011 10000 01001 0000 0000 0000 1000
---------------------------------------------------------------------------------------------

Ex.2.25
2.25.1
a.Ans add $t1,$s0,0xAD10002
b.Ans add $t1,$s0,0xFFFFFFFF
2.25.2
a. Ans add $t1,$s0,0xAD10002
jr $t1
b.Ans add $t1,$s0,0xFFFFFFFF
jr $t1

2.25.3
a. Ans add $t1,$s0,0xAD10002
beq $0, $0, $t1
b.Ans add $t1,$s0,0xFFFFFFFF
beq $0, $0, $t1
2.25.4
a. Ans add $t1,$s0,0xAD10002
beq $0, $0, $t1
b.Ans add $t1,$s0,0xFFFFFFFF
beq $0, $0, $t1
2.25.6
a.Ans $t0 เก็บค่า 0x12345678
b.Ans $t0 เก็บค่า 0x567C
2.25.7
a.Ans int V1,V2;
V1=0x1234;
V1=V1<<16;
V2=x5678 ;
V1=V1|V2;

b.Ans int V1,V2;
V1=0x1234;
V2=x5678 ;
V1=V1|V2;
---------------------------------------------------------------------------------------------

3.10
In a Von Neumann architecture, groups of bits have no instinsic meanings by themselves. What a bit
pattern represents depends entirely on how it is used.The following table shows bit patterns expresses in
hexademical notation.
a. 0x24A60004
b. 0xAFCF0000
3.10.1 What decimal number does the bit pattern represent if it is a two's complement integer? An
unsigned integer?
3.10.2 If this bit pattern is placed into the Instruction Register, What MIPS instruction will be executed?
Ans : a) add $t1,$t1, 0x24A60004
b) add $t1,$t1, 0xAFCF0000
3.10.3 What decimal number does the bit pattern represent if it is a floating-point number? Use the IEEE
754 standard.
Ans : a) 7.199 x 10-17
b) -3.765 x 10-10

The following table shows decimal numbers.
a. -1609.5
b. -938.8125
3.10.4 Write down the binary representation of the decimal number, assuming the IEEE 754 single
precision format.
Ans : a) 1100 0100 1100 1001 0011 0000 0000 0000
b) 1100 0100 0110 1010 1011 0000 0000
3.10.5 Write down the binary representation of the decimal number, assuming the IEEE 754 double
precision format.
Ans : a) 1100 0000 1001 1001 0010 0110 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000
b) 1100 0000 1000 1101 0101 0110 1000 0000 0000 0000 0000 0000 0000 0000 0000
3.10.6 Write down the binary representation of the decimal number assuming it was stored using the
single precision IBM format ( base16, instead of base 2, with 7 bits of exponent).
Ans : a) 1100 1010 1001 0010 0110 0000 0000 0000
b) 1100 1001 1101 0101 0110 1000 0000 0000
--------------------------------------------------------------------------------------------

วันเสาร์ที่ 5 กุมภาพันธ์ พ.ศ. 2554

MIPS programming: Write MIPS programs

1.Translate a C function to calculate a factorial value below into a MIPS
assembly code.

int factorial( int n ){
int f = 1;
if(n == 0) return 1;
do{
f = f * n;
n--;
while( n != 0 );
return f;
}

----------------------
fac:
add $a0 , $zero , 5    # กำหนดค่า parameter ตัวแรก ให้เป็น 5
addi $v0 , $zero , 1
jal loop
jal exit
loop:
mul $v0 , $v0 , $a0 # เก็บค่าของตัวแปรที่ return จากฟังก์ชันให้เป็น $v0
addi $a0 , $a0 , -1
bne $a0 , $zero , loop
jr $ra
exit:


----------------------------
2. Translate a C function to calculate a factorial value below into a MIPS
assembly code.

int fact( int n ){
if ( n == 0 ) return 1;
else return n * fact( n – 1 );
}
-----------------------------
addi $a0,$a0,5
jal factorial
li   $v0, 10         
syscall   
factorial:
addi $sp,$sp,-8
sw $a0,4($sp)
sw $ra,0($sp)
beq $a0,$zero,F1

sub $a0,$a0,1
jal factorial

lw $a0,4($sp)
add $a1,$v0,$zero
jal multiply

lw $ra,0($sp)
lw $a0,4($sp)
add $sp,$sp,8
jr $ra

F1:
addi $v0,$zero,1
add $sp,$sp,8
jr $ra

multiply:
add $t0,$zero,$zero
n_loop:
beq $a1,$zero,m_eol
add $t0,$t0,$a0
subi $a1,$a1,1
j n_loop
m_eol:
add $v0,$t0,$zero
jr $ra




-------------------

วันเสาร์ที่ 25 ธันวาคม พ.ศ. 2553

C programming: Run MARS simulator and Write a Simple program

3. Describe what the three MIPS codes do and include their outputs in your report.


Fibonancci.asm
# Compute several Fibonacci numbers and put in array, then print
.data
fibs:.word   0 : 19         # "array" of words to contain fib values
size: .word  19             # size of "array" (agrees with array declaration)
prompt: .asciiz "How many Fibonacci numbers to generate? (2 <= x <= 19)"
.text
      la   $s0, fibs        # load address of array
      la   $s5, size        # load address of size variable
      lw   $s5, 0($s5)      # load array size
# Optional: user inputs the number of Fibonacci numbers to generate
#pr:   la   $a0, prompt      # load address of prompt for syscall

#      li   $v0, 4           # specify Print String service
#      syscall               # print the prompt string
#      li   $v0, ?????Replace_this_dummy_with_the_correct_numeric_value???????           # specify Read Integer service
#      syscall               # Read the number. After this instruction, the number read is in $v0.
#      bgt  $v0, $s5, pr     # Check boundary on user input -- if invalid, restart
#      blt  $v0, $zero, pr   # Check boundary on user input -- if invalid, restart
#      add  $s5, $v0, $zero  # transfer the number to the desired register     
      li   $s2, 1           # 1 is the known value of first and second Fib. number
      sw   $s2, 0($s0)      # F[0] = 1
      sw   $s2, 4($s0)      # F[1] = F[0] = 1
      addi $s1, $s5, -2     # Counter for loop, will execute (size-2) times
     
      # Loop to compute each Fibonacci number using the previous two Fib. numbers.
loop: lw   $s3, 0($s0)      # Get value from array F[n-2]
      lw   $s4, 4($s0)      # Get value from array F[n-1]
      add  $s2, $s3, $s4    # F[n] = F[n-1] + F[n-2]
      sw   $s2, 8($s0)      # Store newly computed F[n] in array
      addi $s0, $s0, 4      # increment address to now-known Fib. number storage
      addi $s1, $s1, -1     # decrement loop counter
      bgtz $s1, loop        # repeat while not finished
     
      # Fibonacci numbers are computed and stored in array. Print them.
      la   $a0, fibs        # first argument for print (array)
      add  $a1, $zero, $s5  # second argument for print (size)
      jal  print            # call print routine.
      # The program is finished. Exit.
      li   $v0, 10          # system call for exit
      syscall               # Exit!
###############################################################
# Subroutine to print the numbers on one line.
      .data
space:.asciiz  " "          # space to insert between numbers
head: .asciiz  "The Fibonacci numbers are:\n"
      .text
print:add  $t0, $zero, $a0  # starting address of array of data to be printed
      add  $t1, $zero, $a1  # initialize loop counter to array size
      la   $a0, head        # load address of the print heading string
      li   $v0, 4           # specify Print String service
      syscall               # print the heading string
     
out:  lw   $a0, 0($t0)      # load the integer to be printed (the current Fib. number)
      li   $v0, 1           # specify Print Integer service
      syscall               # print fibonacci number
     
      la   $a0, space       # load address of spacer for syscall
      li   $v0, 4           # specify Print String service
      syscall               # print the spacer string
     
      addi $t0, $t0, 4      # increment address of data to be printed
      addi $t1, $t1, -1     # decrement loop counter
      bgtz $t1, out         # repeat while not finished
     
      jr   $ra              # return from subroutine
# End of subroutine to print the numbers on one line

อธิบายโปรแกรม Fibonancci.asm
เป็นการบวกเลข 19 จำนวน  โดยที่กำหนดค่าเริ่มต้นลำดับที่ 1 และ 2 มีค่าเป็น 1  เก็บไว้ใน
Address ตำแหน่งเริ่มต้น และ Address ตำแหน่งเริ่มต้นบวก 4   แล้วนำค่าที่เก็บใน Address 2 ตัวบวกกัน  และนำผลลัพธ์ที่ได้ไปเก็บใน Address ถัดไปอีก 4 ตำแหน่ง  ทำซ้ำไปเรื่อยๆ  จนครบ 19 จำนวน

ผลลัพธ์



row-major.asm
         .data
data:    .word     0 : 256       # storage for 16x16 matrix of words
         .text
         li       $t0, 16        # $t0 = number of rows
         li       $t1, 16        # $t1 = number of columns
         move     $s0, $zero     # $s0 = row counter
         move     $s1, $zero     # $s1 = column counter
         move     $t2, $zero     # $t2 = the value to be stored
#  Each loop iteration will store incremented $t1 value into next element of matrix.
#  Offset is calculated at each iteration. offset = 4 * (row*#cols+col)
#  Note: no attempt is made to optimize runtime performance!
loop:    mult     $s0, $t1       # $s2 = row * #cols  (two-instruction sequence)
         mflo     $s2            # move multiply result from lo register to $s2
         add      $s2, $s2, $s1  # $s2 += column counter
         sll      $s2, $s2, 2    # $s2 *= 4 (shift left 2 bits) for byte offset
         sw       $t2, data($s2) # store the value in matrix element
         addi     $t2, $t2, 1    # increment value to be stored
#  Loop control: If we increment past last column, reset column counter and increment row counter
#                If we increment past last row, we're finished.
         addi     $s1, $s1, 1    # increment column counter
         bne      $s1, $t1, loop # not at end of row so loop back
         move     $s1, $zero     # reset column counter
         addi     $s0, $s0, 1    # increment row counter
         bne      $s0, $t0, loop # not at end of matrix so loop back
#  We're finished traversing the matrix.
         li       $v0, 10        # system service 10 is exit
         syscall                 # we are outta here.     


อธิบายโปรแกรม row-major.asm
            เป็นการสร้าง array ขนาด 16X16  โดยเริ่มใส่ข้อมูลในตำแหน่ง address เริ่มต้น แล้วบวกตำแหน่ง address จากซ้ายไปขวาจนหมดแถว  แล้วกลับมาเริ่มที่ตำแหน่งแรกของแถวถัดไป  ทำซ้ำไปเรื่อยๆ  จนกว่าจะจบแถวสุดท้าย

ผลลัพธ์


column-major.asm


         .data
data:    .word     0 : 256       # 16x16 matrix of words
         .text
         li       $t0, 16        # $t0 = number of rows
         li       $t1, 16        # $t1 = number of columns
         move     $s0, $zero     # $s0 = row counter
         move     $s1, $zero     # $s1 = column counter
         move     $t2, $zero     # $t2 = the value to be stored
#  Each loop iteration will store incremented $t1 value into next element of matrix.
#  Offset is calculated at each iteration. offset = 4 * (row*#cols+col)
#  Note: no attempt is made to optimize runtime performance!
loop:    mult     $s0, $t1       # $s2 = row * #cols  (two-instruction sequence)
         mflo     $s2            # move multiply result from lo register to $s2
         add      $s2, $s2, $s1  # $s2 += col counter
         sll      $s2, $s2, 2    # $s2 *= 4 (shift left 2 bits) for byte offset
         sw       $t2, data($s2) # store the value in matrix element
         addi     $t2, $t2, 1    # increment value to be stored
#  Loop control: If we increment past bottom of column, reset row and increment column
#                If we increment past the last column, we're finished.
         addi     $s0, $s0, 1    # increment row counter
         bne      $s0, $t0, loop # not at bottom of column so loop back
         move     $s0, $zero     # reset row counter
         addi     $s1, $s1, 1    # increment column counter
         bne      $s1, $t1, loop # loop back if not at end of matrix (past the last column)
#  We're finished traversing the matrix.
         li       $v0, 10        # system service 10 is exit
         syscall                 # we are outta here.


อธิบายโปรแกรม column-major.asm
            เป็นการสร้าง array ขนาด 16X16  โดยเริ่มใส่ข้อมูลในตำแหน่ง address เริ่มต้น แล้วบวกตำแหน่ง address
จากบนลงล่าง แล้วกลับมาเริ่มที่ตำแหน่งแรกของคอลัมน์ถัดไป  ทำซ้ำไปเรื่อยๆ  จนกว่าจะจบคอลัมน์สุดท้าย
ผลลัพธ์

---------------------------------------------------------------------------------------


4. In the Fibonnaci.asm program, you are required to do the followings:
- Run the program One Step at a Time from start (at line 7) until the
program finish execute the instruction at line 24 (addi $1, $5, -2) and
describe what happen to the Registers and Data Segment on each
step.

      la   $s0, fibs        # register $s0 เก็บค่า address แรก
      la   $s5, size        # register $s0 เก็บค่า address ทึ่จองไว้
      lw   $s5, 0($s5)      # register $s5 เก็บขนาดของ array
      li   $s2, 1           # register $s2 มีค่าเป็น 1
      sw   $s2, 0($s0)      # data segment ตำแหน่งแรกมีค่าเป็น 1
      sw   $s2, 4($s0)      # data segment ตำแหน่งที่ 2 มีค่าเป็น 1
      addi $s1, $s5, -2   # register $s5  มีค่าเป็น 17

- Set a break point (Bkpt) on the instruction at line 24 and run the
program. Describe what happen in the Data Segment when the
program stop at the breakpoint. Do the same until the execution
finishes.
            Data Segment ตำแน่งถัดไปเก็บค่าผลบวกของตัวเลขสองอันดับก่อนหน้า แล้วเก็บไปเรื่อย ๆ



5. Write a MIP assembly program to calculate sum = 1+2+3+4+...+20 and print
a string “Hello World :)” with the output on screen.


.data
sum: .word 20 # กำหนดจำนวนที่จะให้หาผลบวกเท่ากับ 20
.text
la $s1, sum # register $s1 เก็บค่า sum
lw $s1, 0($s1)

addi $s1 , $s1 , 1
li $s0 , 1
li $s2 , 0

# วนลูป 20 รอบ
loop: add $s2 , $s2 , $s0
addi $s0 , $s0 , 1 # เพิ่มการทำงานที่ละ 1 รอบ
bne $s0 , $s1 , loop # ทำซ้ำจนครบเงื่อนไข

jal print # เรียกฟังก์ชั่น Print

li $v0, 10
syscall # จบการทำงาน

# ฟังก์ชั่น print
.data
head: .asciiz "Summary : "
head2: .asciiz "\nHello World :)\n "
.text
print:add $t0, $zero, $a0
add $t1, $zero, $a1
la $a0, head
li $v0, 4
syscall
out: add $a0 , $s2 , $zero
li $v0, 1
syscall # แสดงผล

out2: la $a0 , head2
li $v0 , 4
syscall
jr $ra

ผลลัพธ์