วันเสาร์ที่ 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

ผลลัพธ์

วันพฤหัสบดีที่ 23 ธันวาคม พ.ศ. 2553

ชีวิตต้องเดินต่อไป

           
มีอยู่วันหนึ่ง เดินอยู่ดีๆ มีคนมาเรียกชื่อ  พอหันกลับไป
ได้เจอเพื่อนเก่า   สุดแสนจะดีใจ                   เป็นไงบ้าง ?
                สบายดีหรือป่าว ? 
                อีกฝ่ายก็ตอบว่า "สบายดี แล้วเธอหละ"  .....
                ...หนึ่งนาที......สองนาที......สามนาที...
                และแล้ว...เวลาก็แยกเราออกจากกัน
             
                ทั้งๆ ที่...
มีเรื่องราวมากมายที่อยากเล่าสู่กันฟัง  ...
                ...แต่ก็ช่างเหอะ ... แค่ได้รู้ว่าเพื่อนสบายดี...แค่นี้ก็สุขใจแล้ว..

               ฉันเดินหน้าต่อไป  ลึกๆในใจเต็มไปด้วยรอยยิ้ม
                        .
                        .
                        .

               หวังว่าทุกคนคงสบายดีนะคะ

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

Memory Management

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main(){
       int num, result = 0;
       int *pnum;
       int i;
       num=5;        // กำหนดจำนวนที่ต้องการจอง
       pnum = (int*)malloc(num*sizeof(int));  // ทำการจองพื้นที่โดยขนาด =ค่า num คูณด้วย int
       if(pnum == NULL)     // ตรวจสอบว่าจองพื้นที่ได้หรือไม่
               printf("memory allocation failed\n");
               else{
                    for(i=0;i<num;i++){    // วนลูปเพื่อนำค่าขนาดของหน่วยความจำมาบวกกัน
                    *(pnum+i) = 0;
                    printf("num[%d] = ",i+1);
                    scanf("%d",&pnum[i]);  // เก็บค่าที่ตัวแปร pnum
                    result = result + *(pnum+i);  // เก็บค่าผลรวมทั้งหมดที่ตัวแปร result
                                       }
                    printf("summation of your numberis %d\n",result);  // แสดงผล
                    free(pnum);  //  คืนค่าหน่วยความจำที่จองทั้งหมด
                    }
       getch();
       }

ผลลัพธ์