commit 46fa27a69c174bfb2dfd026bc8fa859a7b1d5a3e
parent 424d3c5c430b33a7c44d23ebd87d9084efb844a6
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 17 Dec 2020 22:44:59 +0200
new mips assignment
Diffstat:
11 files changed, 305 insertions(+), 18 deletions(-)
diff --git a/c-data-structures/memalloc_ex2.c b/c-data-structures/memalloc_ex2.c
@@ -3,7 +3,7 @@
#include <string.h>
/*
- * Make N student strucutre, allocate memory for them and
+ * Make N student structures, allocate memory for them and
* assign values to them from stdin.
*/
diff --git a/mips-architecture/lab6_ex1.asm b/mips-architecture/lab6_ex1.asm
@@ -0,0 +1,37 @@
+.eqv SYS_PRINT_STRING 4
+.eqv SYS_READ_WORD 5
+.eqv SYS_EXIT 10
+
+.data
+ inputmsg: .asciiz "Number: "
+ evenstr: .asciiz "Even number\n"
+ oddstr: .asciiz "Odd number\n"
+
+.text
+.globl main
+
+main:
+ li $v0, SYS_PRINT_STRING
+ la $a0, inputmsg
+ syscall
+
+ li $v0, SYS_READ_WORD
+ syscall
+
+ # Get the LSB
+ andi $t0, $v0, 1
+ beq $t0, 1, odd
+
+ li $v0, SYS_PRINT_STRING
+ la $a0, evenstr
+ syscall
+ j exit
+
+odd:
+ li $v0, SYS_PRINT_STRING
+ la $a0, oddstr
+ syscall
+
+exit:
+ li $v0, SYS_EXIT
+ syscall
diff --git a/mips-architecture/lab6_ex2.asm b/mips-architecture/lab6_ex2.asm
@@ -0,0 +1,37 @@
+.eqv SYS_PRINT_STRING 4
+.eqv SYS_READ_WORD 5
+.eqv SYS_EXIT 10
+
+.data
+ inputmsg: .asciiz "Number: "
+ ismultstr: .asciiz "Multiple of 4\n"
+ isnmultstr: .asciiz "Not a multiple of 4\n"
+
+.text
+.globl main
+
+main:
+ li $v0, SYS_PRINT_STRING
+ la $a0, inputmsg
+ syscall
+
+ li $v0, SYS_READ_WORD
+ syscall
+
+ # get 2 lowest bits
+ andi $t0, $v0, 3
+ beq $t0, 0, ismult
+
+ li $v0, SYS_PRINT_STRING
+ la $a0, isnmultstr
+ syscall
+ j exit
+
+ismult:
+ li $v0, SYS_PRINT_STRING
+ la $a0, ismultstr
+ syscall
+
+exit:
+ li $v0, SYS_EXIT
+ syscall
diff --git a/mips-architecture/lab6_ex3.asm b/mips-architecture/lab6_ex3.asm
@@ -0,0 +1,43 @@
+.eqv SYS_PRINT_STRING 4
+.eqv SYS_READ_WORD 5
+.eqv SYS_EXIT 10
+
+.data
+ inputmsg: .asciiz "Number: "
+ posstr: .asciiz "Positive\n"
+ negstr: .asciiz "Negative\n"
+
+.text
+.globl main
+
+main:
+ li $v0, SYS_PRINT_STRING
+ la $a0, inputmsg
+ syscall
+
+ li $v0, SYS_READ_WORD
+ syscall
+
+ # Right shift number 31 bits so that the MSB falls
+ # on the LSB's position. Since we're using 2's
+ # complement, if the MSB is 1 the number is negative
+ # and if it's 0 it's positive. After the right shift
+ # `t0` will either be 0 or 1
+
+ srl $t0, $v0, 31
+ beq $t0, 0, positive
+
+ li $v0, SYS_PRINT_STRING
+ la $a0, negstr
+ syscall
+
+ j exit
+
+positive:
+ li $v0, SYS_PRINT_STRING
+ la $a0, posstr
+ syscall
+
+exit:
+ li $v0, SYS_EXIT
+ syscall
diff --git a/mips-architecture/lab6_ex4.asm b/mips-architecture/lab6_ex4.asm
@@ -0,0 +1,62 @@
+.eqv SYS_PRINT_WORD 1
+.eqv SYS_PRINT_STRING 4
+.eqv SYS_READ_WORD 5
+.eqv SYS_EXIT 10
+
+.data
+ inputmsg: .asciiz "Number (0-15): "
+ bounderrstr: .asciiz "Number must be 0-15\n"
+ binstr: .asciiz "Binary: "
+
+.text
+.globl main
+
+main:
+ li $v0, SYS_PRINT_STRING
+ la $a0, inputmsg
+ syscall
+
+ li $v0, SYS_READ_WORD
+ syscall
+ move $t0, $v0
+
+ # Bounds check
+ blt $t0, 0, bounderr
+ bgt $t0, 15, bounderr
+
+ # Init loop counter, We're right shifting in reverse
+ # because otherwise the binary number is going to
+ # show reversed. We assign 3 to `s0` because the range
+ # 0-15 is 4 bits and we'll go from 3 to 0 (4 iterations).
+ # We DON'T start with `s0 = 4` because the last bit
+ # will be eaten.
+ # For example if the number is 3 then
+ #
+ # (0011 >> 3) & 1 = 0000 & 1 = 0
+ # (0011 >> 2) & 1 = 0000 & 1 = 0
+ # (0011 >> 1) & 1 = 0001 & 1 = 1
+ # (0011 >> 0) & 1 = 0011 & 1 = 1
+ #
+ # So the result is 0011 which is the binary form of 3
+ li $s0, 3
+
+binloop:
+ srlv $t1, $t0, $s0 # t1 = t0 >> s0
+ andi $t2, $t1, 1 # t2 = t1 & 1
+
+ li $v0, SYS_PRINT_WORD
+ la $a0, 0($t2)
+ syscall
+
+ beq $s0, 0, exit
+ addi $s0, $s0, -1
+ j binloop
+
+bounderr:
+ li $v0, SYS_PRINT_STRING
+ la $a0, bounderrstr
+ syscall
+
+exit:
+ li $v0, SYS_EXIT
+ syscall
diff --git a/mips-architecture/lab6_ex5.asm b/mips-architecture/lab6_ex5.asm
@@ -0,0 +1,62 @@
+.eqv SYS_PRINT_STRING 4
+.eqv SYS_READ_WORD 5
+.eqv SYS_EXIT 10
+.eqv SYS_PRINT_CHAR 11
+
+.data
+ inputmsg: .asciiz "Number (1-255): "
+ bounderrstr: .asciiz "Number must be 1-255\n"
+ resstr: .asciiz "Hex: 0x"
+ hex: .ascii "0123456789abcdef"
+
+.text
+.globl main
+
+main:
+ li $v0, SYS_PRINT_STRING
+ la $a0, inputmsg
+ syscall
+
+ li $v0, SYS_READ_WORD
+ syscall
+ move $t0, $v0
+
+ # Bounds check
+ blt $t0, 1, bounderr
+ bgt $t0, 255, bounderr
+
+ li $v0, SYS_PRINT_STRING
+ la $a0, resstr
+ syscall
+
+ # Get first 4 bits and shift them 4 bits right
+ # so that `t2` can have their value. For example
+ # if the number is 50 (00110010):
+ #
+ # (00110010 & 0xf0) >> 4 = 00110000 >> 4 = 00000011 = 3
+ andi $t1, $t0, 0xf0
+ srl $t2, $t1, 4
+
+ # Print first portion (4 bits)
+ li $v0, SYS_PRINT_CHAR
+ lb $a0, hex($t2)
+ syscall
+
+ # Get last 4 bits. Using 50 as an example again
+ # 00110010 & 0x0f = 00000010 = 2
+ andi $t1, $t0, 0x0f
+
+ # Grint second portion (4 bits)
+ lb $a0, hex($t1)
+ syscall
+
+ j exit
+
+bounderr:
+ li $v0, SYS_PRINT_STRING
+ la $a0, bounderrstr
+ syscall
+
+exit:
+ li $v0, SYS_EXIT
+ syscall
diff --git a/sh-os1/ex2/bck b/sh-os1/ex2/bck
@@ -19,12 +19,12 @@ main() {
}
usage() {
- echo "usage: ${0##*/} username src dst"
+ echo "usage: ${0##*/} username src dst" 1>&2
exit 1
}
err() {
- echo "${0##*/}: ${1}"
+ echo "${0##*/}: $@" 1>&2
exit 1
}
diff --git a/sh-os1/ex2/createpvs b/sh-os1/ex2/createpvs
@@ -19,12 +19,12 @@ main() {
}
usage() {
- echo "usage: ${0##*/} root_dir num_dbdirs num_datadirs username"
+ echo "usage: ${0##*/} root_dir num_dbdirs num_datadirs username" 1>&2
exit 1
}
err() {
- echo "${0##*/}: ${1}"
+ echo "${0##*/}: $@" 1>&2
exit 1
}
diff --git a/sh-os1/ex2/mfproc b/sh-os1/ex2/mfproc
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+main() {
+ while getopts u:s: arg; do
+ case "${arg}" in
+ u) test ! -z "$(grep -w "^${OPTARG}" /etc/passwd)" ||
+ err "'${OPTARG}' is not in /etc/passwd"
+ uid=$(id -u ${OPTARG}) ;;
+ s) state="${OPTARG}" ;;
+ *) usage
+ esac
+ done
+
+ printf "Name\tPID\tPPID\tUID\tGID\tState\n" | expand -t 16
+ for proc in /proc/*/status; do
+ # call getfield once
+ # handle flags
+ procname=$(getfield "Name" ${proc})
+ procpid=$(getfield "Pid" ${proc})
+ procppid=$(getfield "PPid" ${proc})
+ procuid=$(getfield "Uid:\s*${uid}" ${proc})
+ procgid=$(getfield "Gid" ${proc})
+ procstate=$(getfield "State:\s*${state}" ${proc})
+
+ printf "%s\t%s\t%s\t%s\t%s\t%s\n" \
+ ${procname} ${procpid} ${procppid} ${procuid} ${procgid} ${procstate} |
+ expand -t 16
+
+ done
+}
+
+usage() {
+ echo "usage: ${0##*/} [-u username] [-s S|R|Z]" 1>&2
+ exit 1
+}
+
+err() {
+ echo "${0##*/}: $@" 1>&2
+ exit 1
+}
+
+getfield() {
+ grep -w "^${1}" ${2} | awk '{print $2}'
+}
+
+main "$@"
diff --git a/sh-os1/ex2/searching b/sh-os1/ex2/searching
@@ -46,12 +46,12 @@ main() {
}
usage() {
- echo "usage: ${0##*/} perms days"
+ echo "usage: ${0##*/} perms days" 1>&2
exit 1
}
err() {
- echo "${0##*/}: ${1}"
+ echo "${0##*/}: $@" 1>&2
exit 1
}
diff --git a/sh-os1/ex2/teldb b/sh-os1/ex2/teldb
@@ -15,17 +15,17 @@ main() {
}
usage() {
- echo "usage: ${0##*/} [-a | -l | -n | -c keyword | -d keyword [-b | -r] | -s col]"
- echo ""
- echo "options:"
- echo "-a add a new entry"
- echo "-l list contents of file"
- echo "-n count empty lines and ask if they should be deleted"
- echo "-c filter keyword"
- echo "-d delete lines containing a keyword followed by one of the options below"
- echo " -b replaces line with an empty line"
- echo " -r removes line completely"
- echo "-s sort based on specified column (e.g ${0##*/} -s 3)"
+ echo "usage: ${0##*/} {-a | -l | -n | -c keyword | -d keyword {-b | -r} | -s col}" 1>&2
+ echo "" 1>&2
+ echo "options:" 1>&2
+ echo "-a add a new entry" 1>&2
+ echo "-l list contents of file" 1>&2
+ echo "-n count empty lines and ask if they should be deleted" 1>&2
+ echo "-c filter keyword" 1>&2
+ echo "-d delete lines containing a keyword followed by one of the options below" 1>&2
+ echo " -b replaces line with an empty line" 1>&2
+ echo " -r removes line completely" 1>&2
+ echo "-s sort based on specified column (e.g ${0##*/} -s 3)" 1>&2
exit 1
}