Bläddra i källkod

Init commit: Combine all COD projects to one repo

master
incheckning
f9db247bd6
5 ändrade filer med 685 tillägg och 0 borttagningar
  1. +10
    -0
      .gitignore
  2. +207
    -0
      Q3-qsort/mips1.asm
  3. +207
    -0
      Q3-qsort/quiz3.s
  4. +58
    -0
      Q3-qsort/src/main.c
  5. +203
    -0
      Q5-mips_fpmul/fpmul.asm

+ 10
- 0
.gitignore Visa fil

@@ -0,0 +1,10 @@
# Mars related
*.jar

# Eclipse related
Debug/
Release/
bin/
.settings/
.project
.cproject

+ 207
- 0
Q3-qsort/mips1.asm Visa fil

@@ -0,0 +1,207 @@
.data
v: .word 3, 10, 8, 2, 7, 1, 5, 9, 6, 4

.text
.globl main
main:
# intro - stack-frame
addi $sp, $sp, -12 # main
sw $ra, 8($sp)
sw $fp, 4($sp)
sw $s0, 0($sp)
addi $fp, $sp, 8

li $t0, 10

li $a0, 0
li $a1, 9
la $s0, v
jal qsort

# epilogue - stack-frame
lw $s0, 0($sp)
lw $fp, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra


# swap(int i, int j) : swaps elements of v[]
# at positions i and j
# $a0 = i
# $a1 = j
# $s0 = v
.text
swap:
# intro - stack-frame
addi $sp, $sp, -28
sw $ra, 24($sp)
sw $fp, 20($sp)

sw $s0, 16($sp) # v
sw $t0, 12($sp) # temp
sw $t1, 8($sp) # *v[i]
sw $t2, 4($sp) # *v[j]
sw $t3, 0($sp) # buffer
addi $fp, $sp, 24


move $t1, $s0 # pointer assignements
sll $a0, $a0, 2
add $t1, $t1, $a0
move $t2, $s0
sll $a1, $a1, 2
add $t2, $t2, $a1

lw $t0, 0($t1) # temp = v[i]
lw $t3, 0($t2) # v[i] = v[j]
sw $t3, 0($t1)
sw $t0, 0($t2) # v[j] = temp

# epilogue - stack-frame
lw $t3, 0($sp)
lw $t2, 4($sp)
lw $t1, 8($sp)
lw $t0, 12($sp)
lw $s0, 16($sp)

lw $fp, 20($sp)
lw $ra, 24($sp)
addi $sp, $sp, 28
jr $ra
# partition(int f, int l) : partitions elements in vector v[]
# between positions f and l, using as pivot the element v[l], such
# that
# i < p ==> v[i] <= v[p]
# i > p ==> v[p] < v[i]
# The function returns the position p of the pivot.
# $a0 = f
# $a1 = l
# $s0 = v
# $v0 = p
.text
partition:
# intro - stack-frame
addi $sp, $sp, -40
sw $ra, 36($sp)
sw $fp, 32($sp)

sw $s0, 28($sp) # v
sw $s1, 24($sp) # pivot
sw $s2, 20($sp) # i
sw $s3, 16($sp) # j
sw $s4, 12($sp) # a0 (f)
sw $s5, 8($sp) # a1 (l)
sw $t0, 4($sp)
sw $t1, 0($sp)
addi $fp, $sp, 36

move $s4, $a0
move $s5, $a1
move $t0, $s0 # make *v[l]
move $t1, $a1 # t1 = l
sll $t1, $t1, 2 # t1 *= 4
add $t0, $t0, $t1
lw $s1, 0($t0) # pivot = v[l]
move $s2, $s4 # i = f;

# for () {
move $s3, $s4 # j = f;
_part_loop0:
slt $t0, $s3, $s5 # j < l
beq $t0, $0, _part_loop_end
move $t0, $s0 # make *v[j]
move $t1, $s3
sll $t1, $t1, 2
add $t0, $t0, $t1
lw $t1, 0($t0) # t1 = v[j]
# if (v[j] < pivot)
slt $t0, $t1, $s1
beq $t0, $0, _part_loop_else
move $a0, $s2 # swap (i, j);
move $a1, $s3
jal swap
addi $s2, $s2, 1 # i++
_part_loop_else:
addi $s3, $s3, 1 # j++
j _part_loop0 # }
_part_loop_end:
move $a0, $s2 # swap (i, l)
move $a1, $s5
jal swap
move $v0, $s2 #return i

# epilogue - stack-frame
lw $t1, 0($sp)
lw $t0, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)

lw $fp, 32($sp)
lw $ra, 36($sp)
addi $sp, $sp, 40
jr $ra


# qsort(int f, int l) : sorts the elements of v[]
# between positions f:l
# $a0 = f
# $a1 = l
# $s0 = v
.text
qsort: # qsort entry point
# intro - stack-frame with 4byte local var @ fp
addi $sp, $sp, -28 # qsort
sw $ra, 24($sp)
sw $fp, 20($sp)

sw $s0, 16($sp) # v
sw $s2, 12($sp) # a0 (f)
sw $s3, 8($sp) # a1 (l)
sw $t0, 4($sp)
addi $fp, $sp, 24

move $s2, $a0 # get f, l
move $s3, $a1
# if (f < l) {
slt $t0, $s2, $s3
beq $t0, $0, _qsort_end

move $a0, $s2 # pivot = partition(f, l);
move $a1, $s3
jal partition
sw $v0, 0($sp)

move $a0, $s2 # qsort(f, pivot - 1);
lw $t0, 0($sp)
addi $t0, $t0, -1
move $a1, $t0
jal qsort

lw $t0, 0($sp) # qsort(pivot + 1, l);
addi $t0, $t0, 1
move $a0, $t0
move $a1, $s3
jal qsort
# }
_qsort_end:
# epilogue - stack-frame
lw $t0, 4($sp)
lw $s3, 8($sp)
lw $s2, 12($sp)
lw $s0, 16($sp)

lw $fp, 20($sp)
lw $ra, 24($sp)
addi $sp, $sp, 28
jr $ra # return to caller

+ 207
- 0
Q3-qsort/quiz3.s Visa fil

@@ -0,0 +1,207 @@
.data
v: .word 3, 10, 8, 2, 7, 1, 5, 9, 6, 4


# swap(int i, int j) : swaps elements of v[]
# at positions i and j
# $a0 = i
# $a1 = j
# $s0 = v
.text
swap:
# intro - stack-frame
addi $sp, $sp, -28
sw $ra, 24($sp)
sw $fp, 20($sp)

sw $s0, 16($sp) # v
sw $t0, 12($sp) # temp
sw $t1, 8($sp) # *v[i]
sw $t2, 4($sp) # *v[j]
sw $t3, 0($sp) # buffer
addi $fp, $sp, 24


move $t1, $s0 # pointer assignements
sll $a0, $a0, 2
add $t1, $t1, $a0
move $t2, $s0
sll $a1, $a1, 2
add $t2, $t2, $a1

lw $t0, 0($t1) # temp = v[i]
lw $t3, 0($t2) # v[i] = v[j]
sw $t3, 0($t1)
sw $t0, 0($t2) # v[j] = temp

# epilogue - stack-frame
lw $t3, 0($sp)
lw $t2, 4($sp)
lw $t1, 8($sp)
lw $t0, 12($sp)
lw $s0, 16($sp)

lw $fp, 20($sp)
lw $ra, 24($sp)
addi $sp, $sp, 28
jr $ra

# partition(int f, int l) : partitions elements in vector v[]
# between positions f and l, using as pivot the element v[l], such
# that
# i < p ==> v[i] <= v[p]
# i > p ==> v[p] < v[i]
# The function returns the position p of the pivot.
# $a0 = f
# $a1 = l
# $s0 = v
# $v0 = p
.text
partition:
# intro - stack-frame
addi $sp, $sp, -40
sw $ra, 36($sp)
sw $fp, 32($sp)

sw $s0, 28($sp) # v
sw $s1, 24($sp) # pivot
sw $s2, 20($sp) # i
sw $s3, 16($sp) # j
sw $s4, 12($sp) # a0 (f)
sw $s5, 8($sp) # a1 (l)
sw $t0, 4($sp)
sw $t1, 0($sp)
addi $fp, $sp, 36

move $s4, $a0
move $s5, $a1
move $t0, $s0 # make *v[l]
move $t1, $a1 # t1 = l
sll $t1, $t1, 2 # t1 *= 4
add $t0, $t0, $t1
lw $s1, 0($t0) # pivot = v[l]
move $s2, $s4 # i = f;

# for () {
move $s3, $s4 # j = f;
_part_loop0:
slt $t0, $s3, $s5 # j < l
beq $t0, $0, _part_loop_end
move $t0, $s0 # make *v[j]
move $t1, $s3
sll $t1, $t1, 2
add $t0, $t0, $t1
lw $t1, 0($t0) # t1 = v[j]
# if (v[j] < pivot)
slt $t0, $t1, $s1
beq $t0, $0, _part_loop_else
move $a0, $s2 # swap (i, j);
move $a1, $s3
jal swap
addi $s2, 1 # i++
_part_loop_else:
addi $s3, 1 # j++
j _part_loop0 # }
_part_loop_end:
move $a0, $s2 # swap (i, l)
move $a1, $s5
jal swap
move $v0, $s2 #return i

# epilogue - stack-frame
lw $t1, 0($sp)
lw $t0, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)

lw $fp, 32($sp)
lw $ra, 36($sp)
addi $sp, $sp, 40
jr $ra


# qsort(int f, int l) : sorts the elements of v[]
# between positions f:l
# $a0 = f
# $a1 = l
# $s0 = v
.text
qsort: # qsort entry point
# intro - stack-frame with 4byte local var @ fp
addi $sp, $sp, -28 # qsort
sw $ra, 24($sp)
sw $fp, 20($sp)

sw $s0, 16($sp) # v
sw $s2, 12($sp) # a0 (f)
sw $s3, 8($sp) # a1 (l)
sw $t0, 4($sp)
addi $fp, $sp, 24

move $s2, $a0 # get f, l
move $s3, $a1
# if (f < l) {
slt $t0, $s2, $s3
beq $t0, $0, _qsort_end

move $a0, $s2 # pivot = partition(f, l);
move $a1, $s3
jal partition
sw $v0, 0($sp)

move $a0, $s2 # qsort(f, pivot - 1);
lw $t0, 0($sp)
addi $t0, $t0, -1
move $a1, $t0
jal qsort

lw $t0, 0($sp) # qsort(pivot + 1, l);
addi $t0, $t0, 1
move $a0, $t0
move $a1, $s3
jal qsort
# }
_qsort_end:
# epilogue - stack-frame
lw $t0, 4($sp)
lw $s3, 8($sp)
lw $s2, 12($sp)
lw $s0, 16($sp)

lw $fp, 20($sp)
lw $ra, 24($sp)
addi $sp, $sp, 28
jr $ra # return to caller


.text
.globl main
main:
# intro - stack-frame
addi $sp, $sp, -12 # main
sw $ra, 8($sp)
sw $fp, 4($sp)
sw $s0, 0($sp)
addi $fp, $sp, 8

li $t0, 10

li $a0, 0
li $a1, 9
la $s0, v
jal qsort

# epilogue - stack-frame
lw $s0, 0($sp)
lw $fp, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra

+ 58
- 0
Q3-qsort/src/main.c Visa fil

@@ -0,0 +1,58 @@
//============================================================================
// Name : quiz3.cpp
// Author : Christos Choutouridis
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================



int v[] = {
3, 10, 8, 2, 7, 1, 5, 9, 6, 4
};


void swap (int i, int j) {
int t = v[i];
v[i] = v[j];
v[j] = t;
}

// partition(int f, int l) : permutes the values in vector v[]
// between positions f and l, using as pivot the element v[l], such
// that
// f <= i < p ==> v[i] <= v[p]
// p < i <= l ==> v[p] < v[i]
// The function returns the position p of the pivot.
int partition(int f, int l) {
int pivot = v[l]; // element v[l] is the pivot
int i = f; // i points to the beginning of the vector

// loop over vector
for (int j = f; j < l; j++)
if (v[j] < pivot) // element smaller than pivot
swap(i++,j); // swap it with position pointed by i

swap(i, l ); // swap pivot to its correct position
return (i); // return pivot's position
}

// qsort(int f, int l) : sorts the elements of v[] between positions f:l
void qsort(int f, int l) {
int pivot;
if (f < l) {
pivot = partition(f, l);

qsort(f, pivot - 1); // Before pi
qsort(pivot + 1, l); // After pi
}
}


int main() {
int n = 10;

qsort (0, 9);
return 0;
}

+ 203
- 0
Q5-mips_fpmul/fpmul.asm Visa fil

@@ -0,0 +1,203 @@
.text
.globl main
main:
# intro - stack-frame
addi $sp, $sp, -8 # main
sw $ra, 4($sp)
sw $fp, 0($sp)
addi $fp, $sp, 0 # stack arguments (no)

li $a0, 0xbf800000 # <-- test case goes here
li $a1, 0x3f800000
jal multFloat

# epilogue - stack-frame
lw $fp, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
#
# encodeFloat (sign, exp, mant)
# Arguments
# $a0: The sign
# $a1: The exponent without the bias
# $a2: The normalized mantissa 24bit, with the implied bit
# Retuen
# $v0: The floating point word
encodeFloat:
# intro - stack-frame
addi $sp, $sp, -8 # encodeFloat
sw $ra, 4($sp)
sw $fp, 0($sp)
addi $fp, $sp, 0 # stack arguments (no)
sll $v0, $a0, 31 # v0 = sign << 31
addi $a1, $a1, 127 # v0 |= (exp + offset) << 23
sll $a1, $a1, 23
or $v0, $v0, $a1
li $t0, 0x7FFFFF # v0 |= (mant & 0x7FFFFF);
and $a2, $a2, $t0
or $v0, $v0, $a2

# epilogue - stack-frame
lw $fp, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra

#
# decodeFloat(number)
# arguments
# $a0 : 32bit floating point
# return
# $v0 : sign unsigned int 0 or 1
# $v1 : exponent the value of bits(30:24) - 127, signed integer
# $a1 : mantissa with bits 1 followed by bits (22:0)
decodeFloat:
# intro - stack-frame
addi $sp, $sp, -8 # decodeFloat
sw $ra, 4($sp)
sw $fp, 0($sp)
addi $fp, $sp, 0 # stack arguments (no)

srl $v0, $a0, 31 # sign[v0] = a0 >> 31;

srl $t0, $a0, 23 # exp[v1] = ((a0 >> 23) & 0xFF) - 127;
and $t0, $t0, 0xFF
addi $v1, $t0, -127

li $t0, 0x7FFFFF # Mantissa[a1] = (a0 & 0x7FFFFF) | 0x800000;
and $a1, $a0, $t0
li $t0, 0x800000
or $a1, $a1, $t0

# epilogue - stack-frame
lw $fp, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra # return
#
# multFloat(f1, f2)
# arguments
# $a0 : 32bit floating point 1
# $a1 : 32bit floating point 2
# return
# $v0 : 32bit floating point multiplication result
multFloat:
# s0: sign and argument 2
# s1: exp
# s2: mantissa
# intro - stack-frame
addi $sp, $sp, -20 # multFloat
sw $ra, 16($sp)
sw $fp, 12($sp)
sw $s2, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
addi $fp, $sp, 0 # stack arguments (no)

move $s0, $a1 # save 2nd argument
jal decodeFloat # decodeFloat(arg1) : [v0, v1, a1] <- [s, e, m]
move $t0, $s0 # $s0 change variable to $t0
move $s0, $v0 # load return values to variable regs
move $s1, $v1
move $s2, $a1
move $a0, $t0 # load back argument 2
jal decodeFloat # decodeFloat(arg2) : [v0, v1, a1] <- [s, e, m]
xor $s0, $s0, $v0 # sign = s1 ^ s2
add $s1, $s1, $v1 # exp = e1 + e2
multu $s2, $a1 # mantissa = m1 * m2
mfhi $t4 # t4: mantissa-hi
mflo $t3 # t3: mantissa-low
addi $s2, $zero, 0
li $t9, 0x8000 # if (HI & (1<<15))
and $t8, $t9, $t4
beq $t8, $zero, _multf_l0
# (true)not zero, 48-bits result, normalize
sll $s2, $t4, 8 # mant = HI << 8
srl $t9, $t3, 24 # mant |= LOW >> (32-8)
or $s2, $s2, $t9
addi $s1, $s1, 1 # ++exp, normalize
li $t9, 0x800000 # if (ml & (1<<23)), rounding check
and $t8, $t9, $t3
beq $t8, $zero, _multf_l0_end
addi $s2, $s2, 1 # ++mant, round-up
j _multf_l0_end
_multf_l0:
# (false)zero, 47-bits result
sll $s2, $t4, 9 # mant = HI << 9
srl $t9, $t3, 23 # mant |= LOW >> (32-9)
or $s2, $s2, $t9
li $t9, 0x400000 # if (ml & (1<<22)), rounding check
and $t8, $t9, $t3
beq $t8, $zero, _multf_l0_end
addi $s2, $s2, 1 # ++mant, round-up
_multf_l0_end:

move $a0, $s0
move $a1, $s1
move $a2, $s2
jal encodeFloat # encodeFloat(s, e, m) : float(v0)
# epilogue - stack-frame
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $fp, 12($sp)
lw $ra, 16($sp)
addi $sp, $sp, 20
jr $ra # return
#
# fprod(vector, size)
# Calculates the product x[0] * x[1] * x[2] * ... * x[n-1] as
# Left fold of product operator on a vector of numbers - foldl(*, x)
#
# Arguments
# $a0: Pointer to array x[] in RAM
# $a1: Number of elements
# Return
# $v0: The product
fprod:
# intro - stack-frame
addi $sp, $sp, -20 # fprod
sw $ra, 16($sp)
sw $fp, 12($sp)
sw $s2, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
addi $fp, $sp, 0 # stack arguments (no)

# keep input arguments
move $s0, $a0 # x[]
move $s1, $a1 # n
lw $v0, 0($s0) # Acc = *x
addi $s2, $zero, 1 # i=1
_fprod_loop: # for ( ; i<n ; ) {
bge $s2, $s1, _fprod_loop_end
addi $s0, $s0, 4 # a0 <- *++x
lw $a0, 0($s0)
move $a1, $v0 # a1 <- use prev result
jal multFloat
addi $s2, $s2, 1 # ++i
j _fprod_loop # }
_fprod_loop_end:
# epilogue - stack-frame
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $fp, 12($sp)
lw $ra, 16($sp)
addi $sp, $sp, 20
jr $ra # return


Laddar…
Avbryt
Spara