diff --git a/assignment1/cellular/Makefile b/assignment1/cellular/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/assignment1/cellular/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/assignment1/cellular/c.out b/assignment1/cellular/c.out deleted file mode 100644 index 15a1e8c..0000000 --- a/assignment1/cellular/c.out +++ /dev/null @@ -1,7 +0,0 @@ -Enter world size: Enter rule: Enter how many generations: -0 ..........#......... -1 ###########.######## -2 #...........#....... -3 #.###########.###### -4 #.#...........#..... -5 #.#.###########.#### diff --git a/assignment1/cellular/cellular b/assignment1/cellular/cellular deleted file mode 100755 index a1f0bc0..0000000 Binary files a/assignment1/cellular/cellular and /dev/null differ diff --git a/assignment1/cellular/cellular.c b/assignment1/cellular/cellular.c deleted file mode 120000 index 3656368..0000000 --- a/assignment1/cellular/cellular.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/cellular/files.ln/cellular.c \ No newline at end of file diff --git a/assignment1/cellular/cellular.mk b/assignment1/cellular/cellular.mk deleted file mode 120000 index 3ae73df..0000000 --- a/assignment1/cellular/cellular.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/cellular/files.ln/cellular.mk \ No newline at end of file diff --git a/assignment1/cellular/cellular.s b/assignment1/cellular/cellular.s deleted file mode 100644 index 1cce8e9..0000000 --- a/assignment1/cellular/cellular.s +++ /dev/null @@ -1,575 +0,0 @@ -############################################################################ -# COMP1521 20T2 --- assignment 1: a cellular automaton renderer -# Written by <>, July 2020. - -# This code runs a one-dimensional, three-neighbour cellular automaton. -# It examines its neighbours and its value in the previous generation. -# It then follows a rule entered by the user, which is -# applied to every cell to derive the value for the next generation - - -# Maximum and minimum values for the 3 parameters. - -MIN_WORLD_SIZE = 1 -MAX_WORLD_SIZE = 128 -MIN_GENERATIONS = -256 -MAX_GENERATIONS = 256 -MIN_RULE = 0 -MAX_RULE = 255 - -# Characters used to print alive/dead cells. - -ALIVE_CHAR = '#' -DEAD_CHAR = '.' - -# Maximum number of bytes needs to store all generations of cells. - -MAX_CELLS_BYTES = (MAX_GENERATIONS + 1) * MAX_WORLD_SIZE - - .data - -# `cells' is used to store successive generations. Each byte will be 1 -# if the cell is alive in that generation, and 0 otherwise. - -cells: .space MAX_CELLS_BYTES - - -# Strings for main to check for valid entries: - -prompt_world_size: .asciiz "Enter world size: " -error_world_size: .asciiz "Invalid world size\n" -prompt_rule: .asciiz "Enter rule: " -error_rule: .asciiz "Invalid rule\n" -prompt_n_generations: .asciiz "Enter how many generations: " -error_n_generations: .asciiz "Invalid number of generations\n" - -############################################################################### -# .TEXT
- .text - .globl main - -# Frame: $fp, $ra -# Uses: $a0, $a1, $v0, $t0, $t1, $t2, $t3, $t4, $t5, $t6, $t9 -# Clobbers: $a0, $a1, $t1, $t3 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $s1: rule -# - $s2: n_generations -# - $t9: int g -# - $t0: int reverse -# - $t1: row position/ row offset -# - $t3: col position/ col offsetrow offset -# - $t4: sum of row and col offset -# - $t5: cells array address -# - $t6: value saved in cells - -# Structure: -# main -# -> [prologue] -# -> set_negative_generations -# -> first_generation -# -> loop_run_generation_init -# -> loop_run_generation -# -> check_reverse -# -> reverse_true_init -# -> reverse_true_start -# -> reverse_false_init -# -> reverse_false_start -# -> main_end_1 -# -> main_end_2 -# -> main_end_3 -# -> main_end -# -> [epilogue] - -main: - - # [prologue] set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - la $fp, -4($sp) - addiu $sp, $sp, -8 - - - ##################### Checking for valid entries ################################### - - # Check for valid world size - la $a0, prompt_world_size # printf("Enter world size: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", world_size); - syscall - move $s0, $v0 - - blt $s0, MIN_WORLD_SIZE, main_end_1 # if world_size < MIN_WORLD_SIZE then main_end_1 - bgt $s0, MAX_WORLD_SIZE, main_end_1 # if world_size > MAX_WORLD_SIZE then main_end_1 - - # Check for valid rule - la $a0, prompt_rule # printf("Enter rule: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", rule); - syscall - move $s1, $v0 - - blt $s1, MIN_RULE, main_end_2 # if rule < MIN_RULE then main_end_2 - bgt $s1, MAX_RULE, main_end_2 # if rule > MAX_RULE then main_end_2 - - # Check for valid generations - la $a0, prompt_n_generations # printf("Enter how many generations: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", n_generations); - syscall - move $s2, $v0 - - blt $s2, MIN_GENERATIONS, main_end_3 # if n_generation < MIN_GENERATIONS then main_end_3 - bgt $s2, MAX_GENERATIONS, main_end_3 # if n_generations > MAX_GENERATIONS then main_end_3 - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - ################### Setting up for negative generations ########################### -set_negative_generation: - # negative generations means show the generations in reverse - - li $t0, 0 # int reverse = 0; - bge $s2, 0, first_generation # if n_generations >= 0 then first_generation - - li $t0, 1 # reverse = 1; - mul $s2, $s2, -1 # n_generations = -n_generations - - ########################## Set up generations ################################# -first_generation: - # first generation always only has a only single cell which is alive (in middle) - # cells[0][world_size / 2] = 1; - - # find address of middle cell in first generation - li $t1, 0 # row position = 0 - - li $t2, 2 - div $t3, $s0, $t2 # col position = world_size/2 - - mul $t1, $t1, $s0 # row_offset = row_position * row_size - mul $t3, $t3, 1 # column_offset = col_position * col_size - add $t4, $t1, $t3 # offset = row_offset + col_offset; - - la $t5, cells # get address of 2d array, 'cells' - add $t5, $t5, $t4 # offset = offset + array address - li $t6, 1 - sb $t6, ($t5) # save byte of 1 into address - -loop_run_generation_init: - li $t9, 1 # int g = 1 - -loop_run_generation: - bgt $t9, $s2, check_reverse # if g > n_generations then check_reverse - - # move values into $a registers to pass in arguments - move $a0, $s0 # $a0 <- $s0 (world_size) - move $a1, $s1 # $a1 <- $s1 (rule) - move $a2, $t9 # $a2 <- $t9 (g -> which_generation) - - jal run_generation # run_generation(world_size, g, rule) - add $t9, $t9, 1 # g = g + 1; - b loop_run_generation - -check_reverse: - beq $t0, 0, loop_reverse_false_init # if reverse == 0 then loop_reverse_false_init - -loop_reverse_true_init: - move $t9, $s2 # int g = n_generations - -loop_reverse_true_start: - blt $t9, 0, main_end # if g < 0 then main_end - move $a0, $s0 # $a0 <- s0 (world_size) - move $a1, $t9 # $a1 <- $t9 (int g) - jal print_generation # print_generation(world_size, g); - - addi $t9, $t9, -1 # g = g - 1; - j loop_reverse_true_start - -loop_reverse_false_init: - li $t9, 0 # int g = 0 - -loop_reverse_false_start: - bgt $t9, $s2, main_end # if g > n_generation then main_end - move $a0, $s0 # $a0 <- s0 (world_size) - move $a1, $t9 # $a1 <- $s4 (int g) - jal print_generation # print_generation(world_size, g); - - addi $t9, $t9, 1 # g = g + 1; - j loop_reverse_false_start - - # Note: all main ends at bottom of code - -############################################################################################ - -# RUN_GENERATION - - # Given `world_size', `which_generation', and `rule', calculate - # a new generation according to `rule' and store it in `cells'. - -# Frame: $fp, $ra, $s0, $s1, $s2, $s3, $s4, $s5 -# Uses: $s0, $s1, $s2, $s3, $s4, $s5, $t1, $t2, $t3, $t4, $t5, $t9 -# Clobbers: $a0, $a1, $t2, $t3, $t4, $t5 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $s1: rule -# - $s2: n_generations -# - $s3: int left -# - $s4: int centre -# - $s5: int right -# - $t9: which_generation (int g passed from main) -# - $t1: int x -# - $t2: row position/ row offset -# - $t3: col position/ col offset -# - $t4: sum of row and col offset -# - $t5: address/value stored in cells - -# Structure: -# run_generation -# -> [prologue] -# -> run_generation_loop_init -# -> run_generation_loop_start -# -> run_generation_set_left -# -> run_generation_set_centre -# -> run_generation_set_right -# -> run_generation_state -# -> run_generation_bit -# -> run_generation_set -# -> run_generation_cells -# -> run_generation_set_alive -# -> run_generation_set_dead -# -> run_generation_loop_increment -# -> run_generation_loop_end -# -> [epilogue] - -run_generation: - # [prologue]: set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - sw $s1, -16($sp) # rule - sw $s2, -20($sp) # n_generations - sw $s3, -24($sp) # int left - sw $s3, -28($sp) # int centre - sw $s5, -32($sp) # int right - la $fp, -4($sp) - addiu $sp, $sp, -32 - - # move $a registers back into original registers - move $s0, $a0 # $a0 -> $s0 (world_size) - move $s1, $a1 # $a1 -> $s1 (rule) - move $t9, $a2 # $a2 -> $t9 (int g) - -run_generation_loop_init: - li $t1, 0 # int x -run_generation_loop_start: - bge $t1, $s0, run_generation_loop_end # if x >= world_size then end loop - - # set left cell - li $s3, 0 # int left = 0 - ble $t1, 0, run_generation_set_centre # if x <= 0 then set_centre - -run_generation_set_left: - # find address of LEFT neighbour cell - # left = cells[which_generation - 1][x-1]; - - move $t2, $t9 # find row position - li $t8, 1 # cells[which_generation - 1][...] - sub $t2, $t2, $t8 - - move $t3, $t1 # find column position - sub $t3, $t3, $t8 # cells[...][x-1] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s3, $t5 # save value of left - -run_generation_set_centre: - - # find address of CENTRE neighbour cell - # int centre = cells[which_generation - 1][x]; - - move $t2, $t9 # find row position - li $t8, 1 - sub $t2, $t2, $t8 # cells[which_generation - 1][...] - - move $t3, $t1 # find column position - # cells[...][x] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s4, $t5 # save value of centre - - # set right cell - li $s5, 0 # int right = 0; - move $t2, $s0 # $t2 = world_size - li $t8, 1 - sub $t2, $t2, $t8 # $t2 = world_size - 1; - - bge $t1, $t2, run_generation_state # if x >= world_size-1 then find state - -run_generation_set_right: - - # find address of RIGHT neighbour cell - # right = cells[which_generation - 1][x + 1]; - - move $t2, $t9 # find row position - li $t8, 1 # cells[which_generation - 1][...] - sub $t2, $t2, $t8 - - move $t3, $t1 # find column position - addi $t3, $t3, 1 # cells[...][x + 1] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s5, $t5 # save value of right - -run_generation_state: - # Convert the left, centre, and right states into one value. - - sll $s3, $s3, 2 # left << 2; - sll $s4, $s4, 1 # centre << 1; - sll $s5, $s5, 0 # right << 0; - - li $t2, 0 # int state - or $t2, $t2, $s3 # state |= left - or $t2, $t2, $s4 # state |= centre - or $t2, $t2, $s5 # state |= right - -run_generation_bit: - # check whether bit (1 << state) is set or not in the rule. - # by testing the corresponding bit of the rule number. - - li $t3, 0 # int bit - li $t4, 1 - sllv $t3, $t4, $t2 # bit = 1 << state - -run_generation_set: - - and $t5, $t3, $s1 # int set = bit & rule - -run_generation_cells: - # find address of cells[which_generation][x] - - # find row position - move $t2, $t9 # row/ which generation is g value - - move $t3, $t1 # column - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t6, cells # get address of 2d array - add $t6, $t6, $t4 # add offset to array address - -run_generation_set_alive: - # if the set value not 0, set cell to 'alive' - beq $t5, 0, run_generation_set_dead # if set == 0 then set_dead - - li $t7, 1 - sb $t7, ($t6) # set value/save byte of 1 into address - j run_generation_loop_increment - -run_generation_set_dead: - # if the set value is 0, set cell to 'dead' - li $t7, 0 - sb $t7, ($t6) # set value/save byte of 1 into address - -run_generation_loop_increment: - - addi $t1, $t1, 1 # x = x + 1; - j run_generation_loop_start - -run_generation_loop_end: - - # [epilogue] tear down stack frame - lw $s5, -28($fp) - lw $s4, -24($fp) - lw $s3, -20($fp) - lw $s2, -16($fp) - lw $s1, -12($fp) - lw $s0, -8($fp) - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - jr $ra - -######################################################################################### - -# PRINT_GENERATION - -# Given `world_size', and `which_generation', print out the -# specified generation. - -# Frame: $fp, $ra, $s0 -# Uses: $a0, $a1, $s0, $t1, $t2, $t3, $t4, $t5, $t7, $t9 -# Clobbers: $a0, $t2, $t3 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $t1: int x -# - $t2: world_size/ row position/ row offset -# - $t3: col position/ col offset -# - $t4: sum of row and col offset -# - $t6: address of cells -# - $t7: value saved in cell -# - $t9: which_generation (int g passed from main) - -# Structure: -# print_generation -# -> [prologue] -# -> print_generation_loop_init -# -> print_generation_loop_start -# -> print_alive -# -> print_dead -# -> print_generation_end -# -> [epilogue] - -print_generation: - - # [prologue] set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - la $fp, -4($sp) - addiu $sp, $sp, -12 - - # move $a registers back into original registers - move $s0, $a0 # $a0 -> s0 (world_size) - move $t9, $a1 # $a1 -> $t9 (int g) - - # print which_generation number and tab - - move $a0, $t9 - li $v0, 1 # printf("%d\n", which_generation) - syscall - - li $a0, '\t' # printf("%c", '\t'); - li $v0, 11 - syscall - -print_generation_loop_init: - li $t1, 0 # int x = 0; - -print_generation_loop_start: - bge $t1, $s0, print_generation_end # if x >= world_size then print_generation_end - - # find address of cells[which_generation][x] - # find row position - move $t2, $t9 # row position is 'which_generation' - move $t3, $t1 # column position is x - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t6, cells # get address of 2d array - add $t6, $t6, $t4 # add offset to array address - lb $t7, ($t6) # load 1 or 0 from cell address - - - # check if cell is a 1 or 0 - beq $t7, 0, print_dead # if(cells[which_generation][x])==0 then print_dead - -print_alive: - - li $a0, ALIVE_CHAR # printf("%c", ALIVE_CHAR); - li $v0, 11 - syscall - - addi $t1, $t1, 1 - j print_generation_loop_start - -print_dead: - - li $a0, DEAD_CHAR # printf("%c", DEAD_CHAR); - li $v0, 11 - syscall - - addi $t1, $t1, 1 - j print_generation_loop_start - -print_generation_end: - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - # [epilogue] tear down stack frame - lw $s0, -8($fp) - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - jr $ra - -############################################################################################ - - -main_end_1: - la $a0, error_world_size # printf("Invalid world size"); - li $v0, 4 - syscall - - li $v0, 1 # return 1; - jr $ra - -main_end_2: - la $a0, error_rule # printf("Invalid rule"); - li $v0, 4 - syscall - - li $v0, 1 # return 1; - jr $ra - -main_end_3: - - la $a0, error_n_generations # printf("Invalid number of generations"); - li $v0, 4 - syscall - - li $v0, 1 # return 1; - jr $ra - -main_end: - # [epilogue] tear down stack frame - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - li $v0, 0 # return 0; - jr $ra diff --git a/assignment1/cellular/cellular_tabs.s b/assignment1/cellular/cellular_tabs.s deleted file mode 100644 index 54f9b12..0000000 --- a/assignment1/cellular/cellular_tabs.s +++ /dev/null @@ -1,558 +0,0 @@ -############################################################################ -# COMP1521 20T2 --- assignment 1: a cellular automaton renderer -# Written by <>, July 2020. - -# This code runs a one-dimensional, three-neighbour cellular automaton. -# It examines its neighbours and its value in the previous generation. -# It then follows a rule entered by the user, which is -# applied to every cell to derive the value for the next generation - - -# Maximum and minimum values for the 3 parameters. - -MIN_WORLD_SIZE = 1 -MAX_WORLD_SIZE = 128 -MIN_GENERATIONS = -256 -MAX_GENERATIONS = 256 -MIN_RULE = 0 -MAX_RULE = 255 - -# Characters used to print alive/dead cells. - -ALIVE_CHAR = '#' -DEAD_CHAR = '.' - -# Maximum number of bytes needs to store all generations of cells. - -MAX_CELLS_BYTES = (MAX_GENERATIONS + 1) * MAX_WORLD_SIZE - - .data - -# `cells' is used to store successive generations. Each byte will be 1 -# if the cell is alive in that generation, and 0 otherwise. - -cells: .space MAX_CELLS_BYTES - - -# Strings for main to check for valid entries: - -prompt_world_size: .asciiz "Enter world size: " -error_world_size: .asciiz "Invalid world size\n" -prompt_rule: .asciiz "Enter rule: " -error_rule: .asciiz "Invalid rule\n" -prompt_n_generations: .asciiz "Enter how many generations: " -error_n_generations: .asciiz "Invalid number of generations\n" - -############################################################################### - .text - .globl main - -# MAIN - -# Frame: $fp, $ra -# Uses: $a0, $a1, $v0 -# Clobbers: $a0, $a1 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $s1: rule -# - $s2: n_generations -# - $t9: int g -# - $t0: int reverse - -# Structure: -# main -# -> [prologue] -# -> set_negative_generations -# -> first_generation -# -> loop_run_generation_init -# -> loop_run_generation -# -> check_reverse -# -> reverse_true_init -# -> reverse_true_start -# -> reverse_false_init -# -> reverse_false_start -# -> main_end_1 -# -> main_end_2 -# -> main_end_3 -# -> main_end -# -> [epilogue] - -main: - - # [prologue] set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - la $fp, -4($sp) - addiu $sp, $sp, -8 - - - ##################### Checking for valid entries ################################### - - # Check for valid world size - la $a0, prompt_world_size # printf("Enter world size: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", world_size); - syscall - move $s0, $v0 - - blt $s0, MIN_WORLD_SIZE, main_end_1 # if $s0 < MIN_WORLD_SIZE then main_end_1 - bgt $s0, MAX_WORLD_SIZE, main_end_1 # if $s0 > MAX_WORLD_SIZE then main_end_1 - - # Check for valid rule - la $a0, prompt_rule # printf("Enter rule: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", rule); - syscall - move $s1, $v0 - - blt $s1, MIN_RULE, main_end_2 # if $s1 < MIN_RULE then main_end_2 - bgt $s1, MAX_RULE, main_end_2 # if $s1 > MAX_RULE then main_end_2 - - # Check for valid generations - la $a0, prompt_n_generations # printf("Enter how many generations: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", n_generations); - syscall - move $s2, $v0 - - blt $s2, MIN_GENERATIONS, main_end_3 # if $s2 < MIN_GENERATIONS then main_end_3 - bgt $s2, MAX_GENERATIONS, main_end_3 # if $s2 > MAX_GENERATIONS then main_end_3 - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - ################### Setting up for negative generations ########################### -set_negative_generation: - # negative generations means show the generations in reverse - - li $t0, 0 # int reverse = 0; - bge $s2, 0, first_generation # if n_generations >= 0 then first_generation - - li $t0, 1 # reverse = 1; - mul $s2, $s2, -1 # n_generations = -n_generations - - ########################## Set up generations ################################# -first_generation: - # the first generation always has a only single cell which is alive - # this cell is in the middle of the world - - # find address of middle cell in first generation - li $t1, 0 # row position = 0 - - li $t2, 2 - div $t3, $s0, $t2 # col position = world_size/2 - - mul $t1, $t1, $s0 # row_offset = row_position * row_size - mul $t3, $t3, 1 # column_offset = col_position * col_size - add $t4, $t1, $t3 # offset = row_offset + col_offset; - - la $t5, cells # get address of 2d array, 'cells' - add $t5, $t5, $t4 # offset = offset + array address - li $t6, 1 - sb $t6, ($t5) # save byte of 1 into address - -loop_run_generation_init: - li $t9, 1 # int g = 1 - -loop_run_generation: - bgt $t9, $s2, check_reverse # if $s5 >$s2 then target - - # move values into $a registers to pass in arguments - move $a0, $s0 # $a0 <- $s0 (world_size) - move $a1, $s1 # $a1 <- $s1 (rule) - move $a2, $t9 # $a2 <- $t9 (g (which_generation)) - - jal run_generation - add $t9, $t9, 1 # g = g + 1; - b loop_run_generation - -check_reverse: - bne $t0, 1, loop_reverse_false_init # if reverse != 1 then loop_reverse_false_init - -loop_reverse_true_init: - move $t9, $s2 # int g = n_generations - -loop_reverse_true_start: - blt $t9, 0, main_end # if g < 0 then main_end - move $a0, $s0 # $a0 <- s0 (world_size) - move $a1, $t9 # $a1 <- $t9 (int g) - jal print_generation - - li $t8, 1 - sub $t9, $t9, $t8 # g = g - 1 - j loop_reverse_true_start - -loop_reverse_false_init: - li $t9, 0 # int g = n_generations - -loop_reverse_false_start: - bgt $t9, $s2, main_end # if g > n_generation then main_end - move $a0, $s0 # $a0 <- s0 (world_size) - move $a1, $t9 # $a1 <- $s4 (int g) - jal print_generation - - addi $t9, $t9, 1 # g = g + 1; - j loop_reverse_false_start - - # Note: all main ends at bottom of code - -############################################################################################ - -# RUN_GENERATION - - # Given `world_size', `which_generation', and `rule', calculate - # a new generation according to `rule' and store it in `cells'. - -# Frame: $fp, $ra, $s0, $s1, $s2, $s3, $s4, $s5 -# Uses: $a0, $a1, $v0, $s0, $s1, $s2, $s3, $s4, $s5 -# Clobbers: $a0, $a1 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $s1: rule -# - $s2: n_generations -# - $s3: int left -# - $s4: int centre -# - $s5: int right -# - $t9: which_generation (int g passed from main) -# - $t1: int x - -# Structure: -# run_generation -# -> [prologue] -# -> run_generation_loop_init -# -> run_generation_loop_start -# -> run_generation_set_left -# -> run_generation_set_centre -# -> run_generation_set_right -# -> run_generation_state -# -> run_generation_bit -# -> run_generation_set -# -> run_generation_cells -# -> run_generation_set_alive -# -> run_generation_set_dead -# -> run_generation_loop_increment -# -> run_generation_loop_end -# -> [epilogue] - -run_generation: - # [prologue]: set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - sw $s1, -16($sp) # rule - sw $s2, -20($sp) # n_generations - sw $s3, -24($sp) # int left - sw $s3, -28($sp) # int centre - sw $s5, -32($sp) # int right - la $fp, -4($sp) - addiu $sp, $sp, -32 - - # move $a registers back into original registers - move $s0, $a0 # $a0 -> $s0 (world_size) - move $s1, $a1 # $a1 -> $s1 (rule) - move $t9, $a2 # $a2 -> $t9 (int g) - -run_generation_loop_init: - li $t1, 0 # int x -run_generation_loop_start: - bge $t1, $s0, run_generation_loop_end # if x >= world_size then target - - # set left cell - li $s3, 0 # int left - ble $t1, 0, run_generation_set_centre # if x <= 0 then target - -run_generation_set_left: - # find address of LEFT neighbour cell// cells[which_generation - 1][x-1] - - move $t2, $t9 # find row position - li $t8, 1 # cells[which_generation - 1][...] - sub $t2, $t2, $t8 - - move $t3, $t1 # find column position - sub $t3, $t3, $t8 # cells[...][x-1] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s3, $t5 # save value of left - -run_generation_set_centre: - - # find address of CENTRE neighbour cell// cells[which_generation - 1][x] - - move $t2, $t9 # find row position - li $t8, 1 - sub $t2, $t2, $t8 # cells[which_generation - 1][...] - - move $t3, $t1 # find column position - # cells[...][x] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s4, $t5 # save value of centre - - # set right cell - li $s5, 0 # int left - move $t2, $s0 # $t2 = world_size - li $t8, 1 - sub $t2, $t2, $t8 # world_size - 1; - - bge $t1, $t2, run_generation_state # if x >= world_size-1 then target - -run_generation_set_right: - - # find address of RIGHT neighbour cell// cells[which_generation - 1][x + 1] - - move $t2, $t9 # find row position - li $t8, 1 # cells[which_generation - 1][...] - sub $t2, $t2, $t8 - - move $t3, $t1 # find column position - addi $t3, $t3, 1 # cells[...][x + 1] - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t5, cells # get address of 2d array - add $t5, $t5, $t4 # add offset to array address - - lb $t5, ($t5) # load byte stored in $t5 - - move $s5, $t5 # save value of right - -run_generation_state: - # Convert the left, centre, and right states into one value. - - sll $s3, $s3, 2 # shift left cell 2 bits left - sll $s4, $s4, 1 # shift centre cell 1 bit left - sll $s5, $s5, 0 # shift right cell 0 bits left - - li $t2, 0 # int state - or $t2, $t2, $s3 # state |= left - or $t2, $t2, $s4 # state |= centre - or $t2, $t2, $s5 # state |= right - -run_generation_bit: - # check whether bit (1 << state) is set or not in the rule. - # by testing the corresponding bit of the rule number. - - li $t3, 0 # int bit - li $t4, 1 - sllv $t3, $t4, $t2 # bit = 1 << state - -run_generation_set: - - and $t5, $t3, $s1 # int set = bit & rule - -run_generation_cells: - # find address of cells[which_generation][x] - - # find row position - move $t2, $t9 # row/ which generation is g value - - move $t3, $t1 # column - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t6, cells # get address of 2d array - add $t6, $t6, $t4 # add offset to array address - -run_generation_set_alive: - # if the set value is 1, set cell to 'alive' - beq $t5, 0, run_generation_set_dead # if set == 0 then set_dead - - li $t7, 1 - sb $t7, ($t6) # set value/save byte of 1 into address - j run_generation_loop_increment - -run_generation_set_dead: - # if the set value is 1, set cell to 'dead' - li $t7, 0 - sb $t7, ($t6) # set value/save byte of 1 into address - -run_generation_loop_increment: - - addi $t1, $t1, 1 # $t1 = $t1 + 1 -> x = x + 1 - j run_generation_loop_start - -run_generation_loop_end: - - # [epilogue] tear down stack frame - lw $s5, -28($fp) - lw $s4, -24($fp) - lw $s3, -20($fp) - lw $s2, -16($fp) - lw $s1, -12($fp) - lw $s0, -8($fp) - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - jr $ra - -######################################################################################### - - # PRINT_GENERATION - - # Given `world_size', and `which_generation', print out the - # specified generation. - -# Frame: $fp, $ra, $s0 -# Uses: $a0, $a1, $v0, $s0 -# Clobbers: $a0, $a1 - -# Locals: -# Register | Name/Use -# - $s0: world_size -# - $t1: int x -# - $t9: which_generation (int g passed from main) - -# Structure: -# print_generation -# -> [prologue] -# -> print_generation_loop_init -# -> print_generation_loop_start -# -> print_alive -# -> print_dead -# -> print_generation_end -# -> [epilogue] - -print_generation: - - # [prologue] set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - la $fp, -4($sp) - addiu $sp, $sp, -12 - - # move $a registers back into original registers - move $s0, $a0 # $a0 -> s0 (world_size) - move $t9, $a1 # $a1 -> $t9 (int g) - - # print which_generation number and tab - - move $a0, $t9 - li $v0, 1 # printf("%d\n", which_generation) - syscall - - li $a0, '\t' # printf("%c", '\t'); - li $v0, 11 - syscall - -print_generation_loop_init: - li $t1, 0 # int x = 0 - -print_generation_loop_start: - bge $t1, $s0, print_generation_end # if x >= world_size then target - - # find address of cells[which_generation][x] - # find row position - move $t2, $t9 # row position is g value - - move $t3, $t1 # column position is x - - mul $t2, $t2, $s0 # calculate row offset - mul $t3, $t3, 1 # calculate column offset - add $t4, $t2, $t3 # add row and col offset - - la $t6, cells # get address of 2d array - add $t6, $t6, $t4 # add offset to array address - lb $t7, ($t6) # load 1 or 0 from cell address - - - # check if cell is a 1 or 0 - bne $t7, 1, print_dead # if cell value != 1 then print_dead - -print_alive: - - li $a0, ALIVE_CHAR # printf("%c", '\n'); - li $v0, 11 - syscall - - addi $t1, $t1, 1 - j print_generation_loop_start - -print_dead: - - li $a0, DEAD_CHAR # printf("%c", '\n'); - li $v0, 11 - syscall - - addi $t1, $t1, 1 - j print_generation_loop_start - -print_generation_end: - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - # [epilogue] tear down stack frame - lw $s0, -8($fp) - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - jr $ra - -############################################################################################ - - -main_end_1: - la $a0, error_world_size # printf("Invalid world size"); - li $v0, 4 - syscall - - j main_end - -main_end_2: - la $a0, error_rule # printf("Invalid rule"); - li $v0, 4 - syscall - - j main_end - -main_end_3: - - la $a0, error_n_generations # printf("Invalid number of generations"); - li $v0, 4 - syscall - - j main_end - -main_end: - # [epilogue] tear down stack frame - lw $ra, -4($fp) - la $sp, 4($fp) - lw $fp, ($fp) - - li $v0, 0 - jr $ra diff --git a/assignment1/cellular/mips.out b/assignment1/cellular/mips.out deleted file mode 100644 index 885872c..0000000 --- a/assignment1/cellular/mips.out +++ /dev/null @@ -1,8 +0,0 @@ -The following symbols are undefined: -run_generation -reverse_false -main_end_1 -main_end_2 -main_end_3 - -Enter world size: \ No newline at end of file diff --git a/assignment1/cellular/trial.s b/assignment1/cellular/trial.s deleted file mode 100644 index ddbce33..0000000 --- a/assignment1/cellular/trial.s +++ /dev/null @@ -1,972 +0,0 @@ - la $t1, cells # cells[0][0] - mul $t2, $s5, 1 # $t2 = g*1 - add $t3, $t2, $t1 # cells[g*1 + 0] - sub $t4, $t3, 1 # cells[(g*1-1) + 0] - - mul $t5, $t0, 1 # $t5 = x*1 - add $t6, $t5, $t4 # cells[(g*1-1) + (x*1)] - sub $t7, $t6, 1 # cells[(g*1-1) + (x*1-1)] - - la $t1, cells - #find row position - sub $t2, $s5, 1 # row = g - 1 - #find col position - sub $t3, $t0, 1 # col = x - 1 - #find address of row - mul $t2, $t2, $s0 - #find address of col - mul $t3, $t3, 1 - #add address of row and col - add $t4, $t2, $t3 - #add $t4 to address of cells, save into t4 - add $t4, $t1, $t4 - #load byte into $t4 - lb $s2, ($$t4) - - - - ################################ - - loop_run_gen: - li $s2, 0 # int left = 0 - bge $t0, $s0, end_generation # if (x >= world_size) goto end_generation - - ble $t0, 0, else_run_gen0 # if (x <= 0) goto else_run_gen0 - - la $t1, cells # cells[0][0] - mul $t2, $s5, 1 # $t2 = g*1 - add $t3, $t2, $t1 # cells[g*1 + 0] - sub $t4, $t3, 1 # cells[(g*1-1) + 0] - - mul $t5, $t0, 1 # $t5 = x*1 - add $t6, $t5, $t4 # cells[(g*1-1) + (x*1)] - sub $t7, $t6, 1 # cells[(g*1-1) + (x*1-1)] - - lb $s2, ($t7) # left = cells[which_generation -1][x-1] - # debugged, for left = cells[which_generation -1][x-1] - -######################################33 - - la $t0, cells # gets the base address ofthe array i.e memory gets element cells[0][0] - - div $t1, $s0, 2 # world_size/2 - mul $t2, $t1, 1 # world_size/2 * 4 multiply by 4 to get the single byte - add $t3, $t2, $t0 # cells[0 + world_size/2 * 4] - - li $s3, 1 - sb $s3, ($t3) # cells[0 + world_size / 2] = 1; - -######################################################################## -# COMP1521 20T2 --- assignment 1: a cellular automaton renderer -# -# Written by <>, July 2020. - - -# Maximum and minimum values for the 3 parameters. - -MIN_WORLD_SIZE = 1 -MAX_WORLD_SIZE = 128 -MIN_GENERATIONS = -256 -MAX_GENERATIONS = 256 -MIN_RULE = 0 -MAX_RULE = 255 - -# Characters used to print alive/dead cells. - -ALIVE_CHAR = '#' -DEAD_CHAR = '.' - -# Maximum number of bytes needs to store all generations of cells. - -MAX_CELLS_BYTES = (MAX_GENERATIONS + 1) * MAX_WORLD_SIZE - - .data - -# `cells' is used to store successive generations. Each byte will be 1 -# if the cell is alive in that generation, and 0 otherwise. - -cells: .space MAX_CELLS_BYTES - - -# Some strings you'll need to use: - -prompt_world_size: .asciiz "Enter world size: " -error_world_size: .asciiz "Invalid world size\n" -prompt_rule: .asciiz "Enter rule: " -error_rule: .asciiz "Invalid rule\n" -prompt_n_generations: .asciiz "Enter how many generations: " -error_n_generations: .asciiz "Invalid number of generations\n" - - .text - .globl main - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `main', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `run_generation' FINISHES - # - -main: - - # set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - sw $s1, -16($sp) # rule - sw $s2, -20($sp) # n_generations - sw $s3, -24($sp) # row_size - sw $s4, -28($sp) # col_size - sw $s4, -32($sp) # int g - la $fp, -4($sp) - addiu $sp, $sp, -32 - - - - ##################### Checking for valid entries ################################### - - # Check for valid world size - la $a0, prompt_world_size # printf("Enter world size: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", world_size); - syscall - move $s0, $v0 - - blt $s0, MIN_WORLD_SIZE, main_end_1 # if s < MIN_WORLD_SIZE then target - bgt $s0, MAX_WORLD_SIZE, main_end_1 # if s > MAX_WORLD_SIZE then target - - # Check for valid rule - la $a0, prompt_world_size # printf("Enter rule: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", rule); - syscall - move $s1, $v0 - - blt $s1, MIN_RULE, main_end_2 # if s < MIN_RULE then target - bgt $s1, MAX_RULE, main_end_2 # if s > MAX_RULE then target - - # Check for valid generations - la $a0, prompt_n_generations # printf("Enter how many generations: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", n_generations); - syscall - move $s2, $v0 - - blt $s2, MIN_GENERATIONS, main_end_3 # if s < MIN_GENERATIONS then target - bgt $s2, MAX_GENERATIONS, main_end_3 # if s > MAX_GENERATIONS then target - - ################### Setting up for negative generations ############################## - - li $t0, 0 # int reverse = 0 - bge $s2, 0, row_and_column_size # if $s0 >= 0 then target - - li $t0, 1 # reverse = 1 - mul $s2, $s2, -1 # n_generations = -n_generations - - -row_and_column_size: - # find row size - li $s3, 4 # int row_size = 1 - mul $s3, $s3, $s0 # i.e. row_size = 4 * number of elements in row - # find column size - li $s4, 4 # int col_size = 4 - - - # the first generation always has a only single cell which is alive - # this cell is in the middle of the world -first_generation: - - # find address of middle cell in first generation (row) - - li $t0, 0 # row 1 is in 0th position - - li $t1, 2 # find column position - # i.e. find middle of row (take floor value) - div $t2, $s0, $t1 # $t0 = $s0 / 2 - - - mul $t2, $t2, $s3 # calculate row offset - mul $t3, $t3, $s4 # calculate column offset - add $t5, $t2, $t3 # add row and col offset - - #do i need s7 or t7 here? - la $t6, cells # get address of 2d array - add $t6, $t6, $t5 # add offset to array address - li $t7, 1 - sb $t7, ($t6) # set value/save byte of 1 into address - -loop_run_generation: - - li $s5, 1 # int g = 1 - bgt $s5, $s2, check_reverse # if $s5 >$s2 then target - #add move - jal run_generation - add $s5, $s5, 1 # s5= $s5 + 1 - b loop_run_generation - - -check_reverse: - - bne $t0, 1, reverse_false # if $t0 != 1 then target - -reverse_true: - - - -reverse_false: - - - # replace the syscall below with - # - # li $v0, 0 - # jr $ra - # - # if your code for `main' preserves $ra by saving it on the - # stack, and restoring it after calling `print_world' and - # `run_generation'. [ there are style marks for this ] - - li $v0, 10 - syscall - -################################################################################### -################################################################################### - - # - # Given `world_size', `which_generation', and `rule', calculate - # a new generation according to `rule' and store it in `cells'. - # - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `run_generation', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `run_generation' FINISHES - # - -run_generation: - - # set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s5, -12($sp) # int left - sw $s6, -16($sp) # int centre - sw $s7, -20($sp) # int right - #sw $s8, -24($sp) # state - #sw $s9, -28($sp) # bit - la $fp, -4($sp) - addiu $sp, $sp, -28 - - -run_generation_loop_start: - - # for loop - li $t0, 0 # int x - bge $t0, 0, run_generation_loop_end # if $t0 >= 0 then target - - # set left cell - - li $s5, 0 # int left - ble $t0, 0, run_generation_set_centre # if $t0 <= 0 then target - -run_generation_set_left: - - # find address of left cell// cells[which_generation - 1][x-1] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 - subi $t2, $t2, 1 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s5, $t4 # save address of left - -run_generation_set_centre: - - # find address of CENTRE cell// cells[which_generation - 1][x] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s6, $t4 # save address of left - - - # set right cell - - li $s7, 0 # int left - li $t1, $s0 # $t1 = world_size - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - bge $t0, $t1, run_generation_state # if $t0 <= 0 then target - -run_generation_set_right: - - # find address of right cell// cells[which_generation - 1][x] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 # find column position - addi $t2, $t2, 1 # $t2 = $t2 + 1 - - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s7, $t4 # save address of left - - -run_generation_state: - - # shift left cell 2 bits left - sll $s5, $s5, 2 - - # shift centre cell 1 bit left - sll $s6, $s6, 1 - - # shift right cell 0 bits left - sll $s7, $s7, 2 - - li $t1, 0 # int state - or $t1, $t1, $s5 # $t1 = $t1 | $s5 - or $t1, $t1, $s6 # $t1 = $t1 | $s6 - or $t1, $t1, $s7 # $t1 = $t1 | $s5 - -run_generation_bit: - - li $t2, 0 # int bit - li $t3, 1 - - sllv $t2, $t3, $t1 # $t2 = 1 ($t3) << state ($t1) - -run_generation_set: - - li $t4, 0 # int set - and $t4, $t2, $s1 # set = bit & rule - -run_generation_set_alive: - - bne $t4, 1, run_generation_set_dead # if $t4 != 1 then target - - # find address of cells[which_generation][x] - # find row position - li $t1, $s4 # row/ which generation is g value - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $t5, 0 - sb $t5, ($t4) # set value/save byte of 1 into address - -run_generation_set_dead: - - # find address of cells[which_generation][x] - # find row position - li $t1, $s4 # row/ which generation is g value - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $t5, 1 - sb $t5, ($t4) # set value/save byte of 1 into address - - -run_generation_loop_increment: - - - addi $t0, $t0, 1 # $t0 = $t0 + 1 - j run_generation_loop_start - -run_generation_loop_end: - - jr $ra - - -######################################################################################### -######################################################################################### - - - # - # Given `world_size', and `which_generation', print out the - # specified generation. - # - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `print_generation', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `print_generation' FINISHES - # - -print_generation: - - # - # REPLACE THIS COMMENT WITH YOUR CODE FOR `print_generation'. - # - - jr $ra - - -main_end_1: - la $a0, error_world_size # printf("Invalid world size"); - li $v0, 4 - syscall - - j main_end_common - -main_end_2: - la $a0, error_rule # printf("Invalid rule"); - li $v0, 4 - syscall - - j main_end_common - - -main_end_common: - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - jr $ra # return - - - - - - - - - - -################################################################################## -################################################################################## - - - -######################################################################## -# COMP1521 20T2 --- assignment 1: a cellular automaton renderer -# -# Written by <>, July 2020. - - -# Maximum and minimum values for the 3 parameters. - -MIN_WORLD_SIZE = 1 -MAX_WORLD_SIZE = 128 -MIN_GENERATIONS = -256 -MAX_GENERATIONS = 256 -MIN_RULE = 0 -MAX_RULE = 255 - -# Characters used to print alive/dead cells. - -ALIVE_CHAR = '#' -DEAD_CHAR = '.' - -# Maximum number of bytes needs to store all generations of cells. - -MAX_CELLS_BYTES = (MAX_GENERATIONS + 1) * MAX_WORLD_SIZE - - .data - -# `cells' is used to store successive generations. Each byte will be 1 -# if the cell is alive in that generation, and 0 otherwise. - -cells: .space MAX_CELLS_BYTES - - -# Some strings you'll need to use: - -prompt_world_size: .asciiz "Enter world size: " -error_world_size: .asciiz "Invalid world size\n" -prompt_rule: .asciiz "Enter rule: " -error_rule: .asciiz "Invalid rule\n" -prompt_n_generations: .asciiz "Enter how many generations: " -error_n_generations: .asciiz "Invalid number of generations\n" - - .text - .globl main - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `main', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `run_generation' FINISHES - # - -main: - - # set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s0, -12($sp) # world_size - sw $s1, -16($sp) # rule - sw $s2, -20($sp) # n_generations - sw $s3, -24($sp) # row_size - sw $s4, -28($sp) # col_size - sw $s5, -32($sp) # int g - la $fp, -4($sp) - addiu $sp, $sp, -32 - - - - ##################### Checking for valid entries ################################### - - # Check for valid world size - la $a0, prompt_world_size # printf("Enter world size: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", world_size); - syscall - move $s0, $v0 - - blt $s0, MIN_WORLD_SIZE, main_end_1 # if s < MIN_WORLD_SIZE then target - bgt $s0, MAX_WORLD_SIZE, main_end_1 # if s > MAX_WORLD_SIZE then target - - # Check for valid rule - la $a0, prompt_world_size # printf("Enter rule: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", rule); - syscall - move $s1, $v0 - - blt $s1, MIN_RULE, main_end_2 # if s < MIN_RULE then target - bgt $s1, MAX_RULE, main_end_2 # if s > MAX_RULE then target - - # Check for valid generations - la $a0, prompt_n_generations # printf("Enter how many generations: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", n_generations); - syscall - move $s2, $v0 - - blt $s2, MIN_GENERATIONS, main_end_3 # if s < MIN_GENERATIONS then target - bgt $s2, MAX_GENERATIONS, main_end_3 # if s > MAX_GENERATIONS then target - - ################### Setting up for negative generations ############################## - - li $t0, 0 # int reverse = 0 - bge $s2, 0, row_and_column_size # if $s0 >= 0 then target - - li $t0, 1 # reverse = 1 - mul $s2, $s2, -1 # n_generations = -n_generations - - -row_and_column_size: - # find row size - li $s3, 4 # int row_size = 1 - mul $s3, $s3, $s0 # i.e. row_size = 4 * number of elements in row - # find column size - li $s4, 4 # int col_size = 4 - - - # the first generation always has a only single cell which is alive - # this cell is in the middle of the world -first_generation: - - # find address of middle cell in first generation (row) - - li $t0, 0 # row 1 is in 0th position - - li $t1, 2 # find column position - # i.e. find middle of row (take floor value) - div $t2, $s0, $t1 # $t0 = $s0 / 2 - - - mul $t2, $t2, $s3 # calculate row offset - mul $t3, $t3, $s4 # calculate column offset - add $t5, $t2, $t3 # add row and col offset - - #do i need s7 or t7 here? - la $t6, cells # get address of 2d array - add $t6, $t6, $t5 # add offset to array address - li $t7, 1 - sb $t7, ($t6) # set value/save byte of 1 into address - -loop_run_generation: - - li $s5, 1 # int g = 1 - bgt $s5, $s2, check_reverse # if $s5 >$s2 then target - - # move values into $a registers to pass in arguments - - move $a0, $s0 # $a0 = s0 - move $a1, $s1 # $a1 = $s1 - move $a2, $s4 # $a2 = $s4 - - jal run_generation - add $s5, $s5, 1 # s5= $s5 + 1 - b loop_run_generation - - -check_reverse: - - bne $t0, 1, reverse_false # if $t0 != 1 then target - -reverse_true: - - li $s4, $s2 # int g = n_generations - blt $s4, 0, main_end # if $s4 < 0 then target - - move $a0, $s0 # $a0 = s0 (world_size) - move $a1, $s4 # $a1 = $s4 (int g) - - jal print_generation - - subi $s4, $s4, 1 # s4 = $s4 - 1 - j reverse_true - -reverse_false: - - li $s4, 0 # int g = n_generations - bgt $s4, $s2, main_end # if $s4 < $s2 then target - - move $a0, $s0 # $a0 = s0 (world_size) - move $a1, $s4 # $a1 = $s4 (int g) - - jal print_generation - - addi $s4, $s4, 1 # s4 = $s4 - 1 - j reverse_false - -main_end: - # replace the syscall below with - # - # li $v0, 0 - # jr $ra - # - # if your code for `main' preserves $ra by saving it on the - # stack, and restoring it after calling `print_world' and - # `run_generation'. [ there are style marks for this ] - - li $v0, 10 - syscall - -################################################################################### -################################################################################### - - # - # Given `world_size', `which_generation', and `rule', calculate - # a new generation according to `rule' and store it in `cells'. - # - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `run_generation', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `run_generation' FINISHES - # - -run_generation: - - # set up stack frame - sw $fp, -4($sp) - sw $ra, -8($sp) # return address - sw $s5, -12($sp) # int left - sw $s6, -16($sp) # int centre - sw $s7, -20($sp) # int right - #sw $s8, -24($sp) # state - #sw $s9, -28($sp) # bit - la $fp, -4($sp) - addiu $sp, $sp, -28 - - -run_generation_loop_start: - - # for loop - li $t0, 0 # int x - bge $t0, 0, run_generation_loop_end # if $t0 >= 0 then target - - # set left cell - - li $s5, 0 # int left - ble $t0, 0, run_generation_set_centre # if $t0 <= 0 then target - -run_generation_set_left: - - # find address of left cell// cells[which_generation - 1][x-1] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 - subi $t2, $t2, 1 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s5, $t4 # save address of left - -run_generation_set_centre: - - # find address of CENTRE cell// cells[which_generation - 1][x] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s6, $t4 # save address of left - - - # set right cell - - li $s7, 0 # int left - li $t1, $s0 # $t1 = world_size - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - bge $t0, $t1, run_generation_state # if $t0 <= 0 then target - -run_generation_set_right: - - # find address of right cell// cells[which_generation - 1][x] - - # find row position - li $t1, $s4 # row/ which generation is g value - subi $t1, $t1, 1 # $t1 = $t1 - 1 - - li $t2, $t0 # find column position - addi $t2, $t2, 1 # $t2 = $t2 + 1 - - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $s7, $t4 # save address of left - - -run_generation_state: - - # shift left cell 2 bits left - sll $s5, $s5, 2 - - # shift centre cell 1 bit left - sll $s6, $s6, 1 - - # shift right cell 0 bits left - sll $s7, $s7, 2 - - li $t1, 0 # int state - or $t1, $t1, $s5 # $t1 = $t1 | $s5 - or $t1, $t1, $s6 # $t1 = $t1 | $s6 - or $t1, $t1, $s7 # $t1 = $t1 | $s5 - -run_generation_bit: - - li $t2, 0 # int bit - li $t3, 1 - - sllv $t2, $t3, $t1 # $t2 = 1 ($t3) << state ($t1) - -run_generation_set: - - li $t4, 0 # int set - and $t4, $t2, $s1 # set = bit & rule - -run_generation_set_alive: - - bne $t4, 1, run_generation_set_dead # if $t4 != 1 then target - - # find address of cells[which_generation][x] - # find row position - li $t1, $s4 # row/ which generation is g value - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $t5, 0 - sb $t5, ($t4) # set value/save byte of 1 into address - -run_generation_set_dead: - - # find address of cells[which_generation][x] - # find row position - li $t1, $s4 # row/ which generation is g value - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - li $t5, 1 - sb $t5, ($t4) # set value/save byte of 1 into address - - -run_generation_loop_increment: - - - addi $t0, $t0, 1 # $t0 = $t0 + 1 - j run_generation_loop_start - -run_generation_loop_end: - - jr $ra - - -######################################################################################### -######################################################################################### - - - # - # Given `world_size', and `which_generation', print out the - # specified generation. - # - - # - # REPLACE THIS COMMENT WITH A LIST OF THE REGISTERS USED IN - # `print_generation', AND THE PURPOSES THEY ARE ARE USED FOR - # - # YOU SHOULD ALSO NOTE WHICH REGISTERS DO NOT HAVE THEIR - # ORIGINAL VALUE WHEN `print_generation' FINISHES - # - -print_generation: - - # - # REPLACE THIS COMMENT WITH YOUR CODE FOR `print_generation'. - # - - move $a0, $a0 # move value you're printing (i) to a0 - # li $a0, #printf("%d\n", i) - li $v0, 1 - syscall - - li $a0, '\t' # printf("%c", '\t'); - li $v0, 11 - syscall - - li $t0, 0 # int x = 0 - bge $t0, $a0, print_generation_end # if $t0 >= $t1 then target - - - # find address of cells[which_generation][x] - # find row position - li $t1, $a1 # row/ which generation is g value - - li $t2, $t0 # find column position - - mul $t1, $t1, $s3 # calculate row offset - mul $t2, $t2, $s4 # calculate column offset - add $t3, $t2, $t1 # add row and col offset - - la $t4, cells # get address of 2d array - add $t4, $t4, $t3 # add offset to array address - - - - - bne $t0, $t1, target # if $t0 != $t1 then target - - -print_generation_end: - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - - jr $ra - - - -############################################################################################ - - -main_end_1: - la $a0, error_world_size # printf("Invalid world size"); - li $v0, 4 - syscall - - j main_end_common - -main_end_2: - la $a0, error_rule # printf("Invalid rule"); - li $v0, 4 - syscall - - j main_end_common - - -main_end_common: - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - jr $ra # return \ No newline at end of file diff --git a/assignment2/smips/0.hex b/assignment2/smips/0.hex deleted file mode 100644 index bd0b970..0000000 --- a/assignment2/smips/0.hex +++ /dev/null @@ -1 +0,0 @@ -0\n28939382 diff --git a/assignment2/smips/beq.hex b/assignment2/smips/beq.hex deleted file mode 100644 index 6ffe675..0000000 --- a/assignment2/smips/beq.hex +++ /dev/null @@ -1,9 +0,0 @@ -34090001 -92020 -34020001 -c -3404000a -3402000b -c -34080001 -1128FFE8 \ No newline at end of file diff --git a/assignment2/smips/beq.s b/assignment2/smips/beq.s deleted file mode 100644 index 299ee7c..0000000 --- a/assignment2/smips/beq.s +++ /dev/null @@ -1,18 +0,0 @@ -main: - - li $t1, 1 - move $a0, $t1 # printf("%d", 1); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - -bne1: - - li $t0, 1 - beq $t1, $t0, main # if != $t1 then target - - - jr $ra diff --git a/assignment2/smips/bne.hex b/assignment2/smips/bne.hex deleted file mode 100644 index 0ca5a2d..0000000 --- a/assignment2/smips/bne.hex +++ /dev/null @@ -1,9 +0,0 @@ -34090001 -92020 -34020001 -c -3404000a -3402000b -c -34080000 -1528FFF1 \ No newline at end of file diff --git a/assignment2/smips/bne.s b/assignment2/smips/bne.s deleted file mode 100644 index 74c8f1a..0000000 --- a/assignment2/smips/bne.s +++ /dev/null @@ -1,18 +0,0 @@ -main: - - li $t1, 1 - move $a0, $t1 # printf("%d", 1); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - -bne1: - - li $t0, 0 - bne $t1, $t0, main # if != $t1 then target - - - jr $ra diff --git a/assignment2/smips/examples/42.hex b/assignment2/smips/examples/42.hex deleted file mode 100644 index f082a0e..0000000 --- a/assignment2/smips/examples/42.hex +++ /dev/null @@ -1,6 +0,0 @@ -3404002a -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/42.s b/assignment2/smips/examples/42.s deleted file mode 100644 index 9cae13b..0000000 --- a/assignment2/smips/examples/42.s +++ /dev/null @@ -1,8 +0,0 @@ -main: - li $a0, 42 # printf("%d", 42); - li $v0, 1 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - jr $ra diff --git a/assignment2/smips/examples/4242424242.hex b/assignment2/smips/examples/4242424242.hex deleted file mode 100644 index c5fdbb1..0000000 --- a/assignment2/smips/examples/4242424242.hex +++ /dev/null @@ -1,7 +0,0 @@ -3c010287 -342457b2 -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/4242424242.s b/assignment2/smips/examples/4242424242.s deleted file mode 100644 index 0e1f04c..0000000 --- a/assignment2/smips/examples/4242424242.s +++ /dev/null @@ -1,8 +0,0 @@ -main: - li $a0, 42424242 # printf("%d", 42424242); - li $v0, 1 - syscall - li $a0,'\n' # printf("%c", '\n'); - li $v0, 11 - syscall - jr $ra diff --git a/assignment2/smips/examples/alphabet.hex b/assignment2/smips/examples/alphabet.hex deleted file mode 100644 index 0bdbf14..0000000 --- a/assignment2/smips/examples/alphabet.hex +++ /dev/null @@ -1,10 +0,0 @@ -3402000b -3408005a -21080001 -34040041 -c -20840001 -88482a -1520fffd -3404000a -c diff --git a/assignment2/smips/examples/alphabet.s b/assignment2/smips/examples/alphabet.s deleted file mode 100644 index c827fb8..0000000 --- a/assignment2/smips/examples/alphabet.s +++ /dev/null @@ -1,13 +0,0 @@ -main: - li $v0, 11 - li $t0, 'Z' - addi $t0, $t0, 1 - li $a0, 'A' -start: - syscall - addi $a0, $a0, 1 - slt $t1, $a0, $t0 - bne $t1, $0, start - li $a0, '\n' - syscall - jr $ra diff --git a/assignment2/smips/examples/bad_syscall.hex b/assignment2/smips/examples/bad_syscall.hex deleted file mode 100644 index 42535d9..0000000 --- a/assignment2/smips/examples/bad_syscall.hex +++ /dev/null @@ -1,8 +0,0 @@ -34021092 -c -3404002a -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/bad_syscall.s b/assignment2/smips/examples/bad_syscall.s deleted file mode 100644 index ffc0525..0000000 --- a/assignment2/smips/examples/bad_syscall.s +++ /dev/null @@ -1,9 +0,0 @@ -main: - li $v0, 4242 - syscall - li $a0, 42 # printf("%d", 42); - li $v0, 1 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall diff --git a/assignment2/smips/examples/exit.hex b/assignment2/smips/examples/exit.hex deleted file mode 100644 index 21d9ac8..0000000 --- a/assignment2/smips/examples/exit.hex +++ /dev/null @@ -1,14 +0,0 @@ -3404002a -34020001 -c -3404000a -3402000b -c -3402000a -c -34040018 -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/exit.s b/assignment2/smips/examples/exit.s deleted file mode 100644 index bc8d2b0..0000000 --- a/assignment2/smips/examples/exit.s +++ /dev/null @@ -1,15 +0,0 @@ -main: - li $a0, 42 # printf("%d", 42); - li $v0, 1 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - li $v0, 10 # exit() - syscall - li $a0, 24 # printf("%d", 24); - li $v0, 1 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall diff --git a/assignment2/smips/examples/hello.hex b/assignment2/smips/examples/hello.hex deleted file mode 100644 index a2478f6..0000000 --- a/assignment2/smips/examples/hello.hex +++ /dev/null @@ -1,13 +0,0 @@ -3402000b -34040048 -c -34040065 -c -3404006c -c -3404006c -c -3404006f -c -3404000a -c diff --git a/assignment2/smips/examples/hello.s b/assignment2/smips/examples/hello.s deleted file mode 100644 index 51e0bf3..0000000 --- a/assignment2/smips/examples/hello.s +++ /dev/null @@ -1,15 +0,0 @@ -main: - li $v0, 11 - li $a0, 'H' - syscall - li $a0, 'e' - syscall - li $a0, 'l' - syscall - li $a0, 'l' - syscall - li $a0, 'o' - syscall - li $a0, '\n' - syscall - jr $ra diff --git a/assignment2/smips/examples/loop.hex b/assignment2/smips/examples/loop.hex deleted file mode 100644 index 7392f19..0000000 --- a/assignment2/smips/examples/loop.hex +++ /dev/null @@ -1,9 +0,0 @@ -3408000a -82020 -34020001 -c -3404000a -3402000b -c -2108ffff -1500fff9 diff --git a/assignment2/smips/examples/loop.s b/assignment2/smips/examples/loop.s deleted file mode 100644 index beb7395..0000000 --- a/assignment2/smips/examples/loop.s +++ /dev/null @@ -1,13 +0,0 @@ -# print integers 10..1 on separate lines -main: - li $t0, 10 -loop: - move $a0, $t0 - li $v0, 1 - syscall - li $a0, '\n' - li $v0, 11 - syscall - add $t0, $t0, -1 - bne $t0, $0, loop - jr $ra diff --git a/assignment2/smips/examples/r0.hex b/assignment2/smips/examples/r0.hex deleted file mode 100644 index ca5fdc8..0000000 --- a/assignment2/smips/examples/r0.hex +++ /dev/null @@ -1,8 +0,0 @@ -3409002a -1290020 -2020 -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/r0.s b/assignment2/smips/examples/r0.s deleted file mode 100644 index aa75464..0000000 --- a/assignment2/smips/examples/r0.s +++ /dev/null @@ -1,10 +0,0 @@ -main: - li $t1, 42 - add $0, $t1, $t1 - add $a0, $0, $0 - li $v0, 1 - syscall - li $a0, '\n' - li $v0, 11 - syscall - jr $ra diff --git a/assignment2/smips/examples/square.hex b/assignment2/smips/examples/square.hex deleted file mode 100644 index 230d704..0000000 --- a/assignment2/smips/examples/square.hex +++ /dev/null @@ -1,10 +0,0 @@ -34100004 -34110003 -72108002 -72318802 -2302020 -34020001 -c -3404000a -3402000b -c diff --git a/assignment2/smips/examples/square.s b/assignment2/smips/examples/square.s deleted file mode 100644 index 8dd96b9..0000000 --- a/assignment2/smips/examples/square.s +++ /dev/null @@ -1,13 +0,0 @@ -# calculate 3*3 + 4*4 -main: - li $s0, 4 - li $s1, 3 - mul $s0, $s0, $s0 - mul $s1, $s1, $s1 - add $a0, $s1, $s0 - li $v0, 1 - syscall - li $a0, '\n' - li $v0, 11 - syscall - jr $ra diff --git a/assignment2/smips/examples/triangle.hex b/assignment2/smips/examples/triangle.hex deleted file mode 100644 index bc7df78..0000000 --- a/assignment2/smips/examples/triangle.hex +++ /dev/null @@ -1,15 +0,0 @@ -34080005 -34090001 -3402000b -340a0000 -3404002a -c -214a0001 -3404000a -149582a -140bfffb -3404000a -c -21290001 -128582a -140bfff5 diff --git a/assignment2/smips/examples/triangle.s b/assignment2/smips/examples/triangle.s deleted file mode 100644 index 4549f58..0000000 --- a/assignment2/smips/examples/triangle.s +++ /dev/null @@ -1,19 +0,0 @@ -main: - li $t0, 5 - li $t1, 1 - li $v0, 11 -loop0: - li $t2, 0 -loop1: - li $a0, '*' - syscall - add $t2, $t2, 1 - li $a0, '\n' - slt $t3, $t2, $t1 - bne $t3, 0, loop1 - li $a0, '\n'; - syscall - add $t1, $t1, 1 - slt $t3, $t1, $t0 - bne $t3, 0, loop0 - jr $31 diff --git a/assignment2/smips/slt.hex b/assignment2/smips/slt.hex deleted file mode 100644 index 029c31d..0000000 --- a/assignment2/smips/slt.hex +++ /dev/null @@ -1,16 +0,0 @@ -34090001 -92020 -34020001 -c -3404000a -3402000b -c -3c01ffff -3428fffd -10b482a -82020 -34020001 -c -3404000a -3402000b -c \ No newline at end of file diff --git a/assignment2/smips/slt.s b/assignment2/smips/slt.s deleted file mode 100644 index e1ae1af..0000000 --- a/assignment2/smips/slt.s +++ /dev/null @@ -1,23 +0,0 @@ -main: - - li $t1, 1 - move $a0, $t1 # printf("%d", 1); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - li $t0, -3 - slt $t1, $t0, $t3 - - move $a0, $t0 - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - jr $ra diff --git a/assignment2/smips/smips b/assignment2/smips/smips deleted file mode 100755 index 22a3ccb..0000000 Binary files a/assignment2/smips/smips and /dev/null differ diff --git a/assignment2/smips/smips.c b/assignment2/smips/smips.c deleted file mode 100644 index 594a1ae..0000000 --- a/assignment2/smips/smips.c +++ /dev/null @@ -1,382 +0,0 @@ - -// COMP1521 20T2 Assignment 2 - smips.c -// Written by Rifa Jamal z5311190 on August 2020 - - -////////////////////////////////////////////////////////////////////////// -// SMIPS.C // -// // -// This program is an emulator for a small simple subset of the MIPS. // -// // -// The input for smips.c will be the 32-bit instruction // -// codes for MIPS instructions as hexadecimal numbers. // -// // -// The output from smips.c is: // -// The instruction (assempler) corresponding to each instruction code // -// The output produced by syscalls when executing the MIPS instructions.// -// The register values when the program terminates. // -// // -////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include - -#define MAX_INSTRUCTION_CODES 1000 -#define MAX_REGISTERS 32 - -// R-Type Instruction Defines -#define ADD 32 -#define SUB 34 -#define AND 36 -#define OR 37 -#define SLT 42 - -// Syscall Defines -#define SYSCALL 12 -#define REG_V0 2 -#define REG_A0 4 - -// I/J-Type Instruction Defines -#define MUL 28 -#define BEQ 4 -#define BNE 5 -#define ADDI 8 -#define SLTI 10 -#define ANDI 12 -#define ORI 13 -#define LUI 15 - -// Instruction Component Defines -#define OPCODE_SHIFT 26 -#define REG_D_SHIFT 11 -#define REG_S_SHIFT 21 -#define REG_T_SHIFT 16 -#define REG_MASK 0x1F -#define FUNCT_MASK 0x3F -#define IMM_MASK 0xFFFF - - -// Functions for Printing and Executing Mips Code -void decode_instruction(uint32_t instruction); -int execute_instruction(uint32_t instruction, int32_t registers[32], int *k); - -// Function to check for valid instruction inputs -int check_valid(uint32_t instruction); - - -////////////////////////////////////////////////////////////////////// -// MAIN // -// // -// Main function reads the hexadecimal input into an array 'codes' // -// which then calls relevant functions to produce the output. // -// // -////////////////////////////////////////////////////////////////////// - - -int main(int arg, char *argv[]) { - - // Read in command line argument - FILE *stream = fopen(argv[1], "r"); - // Check if file read was successful - if (stream == NULL) { - perror(argv[1]); - exit(1); - } - - // codes holds the hexadecimal values from input - uint32_t codes[MAX_INSTRUCTION_CODES] = { 0 }; - - // registers is an array of registers - int32_t registers[MAX_REGISTERS] = { 0 }; - - // loop - if successful, reads from stream: hex into 'codes' array - // loop also makes sure of no overflow. - int n_instructions = 0; - while (n_instructions < MAX_INSTRUCTION_CODES - && fscanf(stream, "%x", &codes[n_instructions]) == 1) { - - // Error Check: Valid instruction code - // If instruction not valid, print error message and exit - if (check_valid(codes[n_instructions]) != 0) { - printf("%s:%d: invalid instruction code: %08X\n", - argv[1], (n_instructions + 1), codes[n_instructions]); - exit(1); - } - - n_instructions++; - } - - // Error Check: if fscanf does not successfully read file, - // print error message and exit program - if ((fscanf(stream, "%x", &codes[n_instructions]) == 0)) { - printf("%s:1: invalid instruction code: %08X\n", - argv[1], codes[n_instructions]); - exit(1); - } - - // Printing the hexadecimal instructions as Mips code - printf("Program\n"); - for (int j = 0; j < n_instructions; j++) { - if (j < 10 ) { - printf(" %d: ", j); - } else { - printf(" %d: ", j); - } - decode_instruction(codes[j]); - printf("\n"); - } - - // Executing the Mips code and printing the output - printf("Output\n"); - // for coniditon makes sure that the program line counter is within boundaries - // such as after executing beq and bne instructions. - for (int line = 0; line >= 0 && line < n_instructions; line++) { - - // If the function returns a value other than 0, either: - // syscall called exit, or - // syscall was given an invalid argument - if (execute_instruction(codes[line], registers, &line) != 0) { - break; - } - // register $0 will always be the value 0 - // even if the value was changed in the instructions - registers[0] = 0; - } - - // Printing all non-zero register values after execution - printf("Registers After Execution\n"); - for (int r_counter = 1; r_counter < MAX_REGISTERS; r_counter++) { - if (registers[r_counter] != 0) { - printf("$%-2d = %d\n", r_counter, registers[r_counter]); - } - } - -} - -////////////////////////////////////////////////////////////////// -// CHECK_VALID // -// // -// check_valid checks that if given a hexadecimal number that // -// does not correspond to an instruction in the MIPS subset, // -// 1 will be returned and a error message will be printed. // -// // -////////////////////////////////////////////////////////////////// - - -int check_valid(uint32_t instruction) { - - // Find componenets of instruction - // Operation code - first 6 bits - uint32_t opcode = (instruction >> OPCODE_SHIFT); - // Function code - last 6 bits - uint32_t funct = instruction & FUNCT_MASK; - - // Check if valid R-type instructions or Syscall - if (opcode == 0) { - if (funct != ADD - && funct != SUB - && funct != AND - && funct != OR - && funct != SLT - && funct != SYSCALL) { - - return 1; - } - // Check if valid I or J Type instruction - } else if (opcode != MUL - && opcode != BEQ - && opcode != BNE - && opcode != ADDI - && opcode != SLTI - && opcode != ANDI - && opcode != ORI - && opcode != LUI ) { - return 1; - } - - return 0; - -} - -////////////////////////////////////////////////////////////////// -// DECODE_INSTRUCTIONS // -// // -// This function decodes then prints the // -// instruction corresponding to each instruction code. // -// // -////////////////////////////////////////////////////////////////// - -void decode_instruction(uint32_t instruction) { - - // Find componenets of instruction - uint32_t opcode = (instruction >> OPCODE_SHIFT); // first 6 bits - uint32_t d = (instruction >> REG_D_SHIFT) & REG_MASK; // second 5 bits - uint32_t s = (instruction >> REG_S_SHIFT) & REG_MASK; // third 5 bits - uint32_t t = (instruction >> REG_T_SHIFT) & REG_MASK; // fourth 5 bits - uint32_t funct = instruction & FUNCT_MASK; // last 6 bits - int16_t i = (instruction & IMM_MASK); // immediate/ address - - // Check for R-type instructions or Syscall - if (opcode == 0) { - // Check variants of instructions that have a '000000' opcode - if (funct == ADD) { - // add $d, $s, $t - printf("add $%d, $%d, $%d", d, s, t); - } else if (funct == SUB) { - // sub $d, $s, $t - printf("sub $%d, $%d, $%d", d, s, t); - } else if (funct == AND) { - // and $d, $s, $t - printf("and $%d, $%d, $%d", d, s, t); - } else if (funct == OR) { - // or $d, $s, $t - printf("or $%d, $%d, $%d", d, s, t); - } else if (funct == SLT) { - // slt $d, $s, $t - printf("slt $%d, $%d, $%d", d, s, t); - } else if (funct == SYSCALL) { - printf("syscall"); - } - - // Check for I/J-type instructions - } else if (opcode == MUL) { - // mul $d, $s, $t - printf("mul $%d, $%d, $%d", d, s, t); - } else if (opcode == BEQ) { - // beq $s, $t, I - printf("beq $%d, $%d, %d", s, t, i); - } else if (opcode == BNE) { - // bne $s, $t, I - printf("bne $%d, $%d, %d", s, t, i); - } else if (opcode == ADDI) { - // addi $t, $s, I - printf("addi $%d, $%d, %d", t, s, i); - } else if (opcode == SLTI) { - // slti $t, $s, I - printf("slti $%d, $%d, %d", t, s, i); - } else if (opcode == ANDI) { - // andi $t, $s, I - printf("andi $%d, $%d, %d", t, s, i); - } else if (opcode == ORI) { - // ori $t, $s, I - printf("ori $%d, $%d, %d", t, s, i); - } else if (opcode == LUI) { - // lui $t, I - printf("lui $%d, %d", t, i); - } - -} - -///////////////////////////////////////////////////////////////////// -// EXECUTE_INSTRUCTIONS // -// // -// This function runs through and executes the Mips instructions. // -// If there is a syscall, a relevant output will be produced // -// based on the request. // -// // -///////////////////////////////////////////////////////////////////// - -int execute_instruction(uint32_t instruction, int32_t registers[32], int *line) { - - // Find componenets of instruction - uint32_t opcode = (instruction >> OPCODE_SHIFT); // first 6 bits - uint32_t d = (instruction >> REG_D_SHIFT) & REG_MASK; // second 5 bits - uint32_t s = (instruction >> REG_S_SHIFT) & REG_MASK; // third 5 bits - uint32_t t = (instruction >> REG_T_SHIFT) & REG_MASK; // fourth 5 bits - uint32_t funct = instruction & FUNCT_MASK; // last 6 bits - int16_t i = (instruction & IMM_MASK); // immediate/ address - - // Check for R-type instructions or Syscall - if (opcode == 0) { - // Check variants of instructions that have a '000000' opcode - if (funct == ADD) { - // add $d, $s, $t - registers[d] = registers[s] + registers[t]; - - } else if (funct == SUB) { - // sub $d, $s, $t - registers[d] = registers[s] - registers[t]; - - } else if (funct == AND) { - // and $d, $s, $t - registers[d] = registers[s] & registers[t]; - - } else if (funct == OR) { - // or $d, $s, $t - registers[d] = registers[s] | registers[t]; - - } else if (funct == SLT) { - // slt $d, $s, $t - registers[d] = registers[s] < registers[t]; - } else if (funct == SYSCALL) { - // Syscall instruction - // find v0 and a0 - int32_t v0 = registers[REG_V0]; - int32_t a0 = registers[REG_A0]; - - if (v0 == 1) { - // Print integer (in $a0 register) - printf("%d", a0); - } else if (v0 == 10) { - // Exit - return 1; - } else if (v0 == 11) { - // Print character (in $a0 register) - // note: print lowest 8 bits for character - printf("%c", (a0 & 0xFF)); - } else { - // Else if system call is not valid, - // print an error message and return to main - printf("Unknown system call: %d\n", v0); - return 1; - } - } - // Check for I/J-type instructions - } else if (opcode == MUL) { - // mul $d, $s, $t - registers[d] = registers[s] * registers[t]; - - } else if (opcode == BEQ) { - // beq $s, $t, I - if (registers[s] == registers[t]) { - // branch up or down instructions given by i - // line is the program counter (PC) - *line = *line + (i-1); - } - - } else if (opcode == BNE) { - // bne $s, $t, I - if (registers[s] != registers[t]) { - // branch up or down instructions given by i - // line is the program counter (PC) - *line = *line + (i-1); - } - - } else if (opcode == ADDI) { - // addi $t, $s, I - registers[t] = registers[s] + i; - - } else if (opcode == SLTI) { - // slti $t, $s, I - registers[t] = registers[s] < i; - - } else if (opcode == ANDI) { - // andi $t, $s, I - registers[t] = registers[s] & i; - - } else if (opcode == ORI) { - // ori $t, $s, I - registers[t] = registers[s] | i; - - } else if (opcode == LUI) { - // lui $t, I - // shift on I as an unsigned value - uint32_t unsigned_i = (instruction & IMM_MASK); - registers[t] = unsigned_i << 16; - } - - return 0; - -} diff --git a/assignment2/smips/test.hex b/assignment2/smips/test.hex deleted file mode 100644 index 25a5cf5..0000000 --- a/assignment2/smips/test.hex +++ /dev/null @@ -1 +0,0 @@ -dvskn JDL vlkD c diff --git a/assignment2/smips/without_array.c b/assignment2/smips/without_array.c deleted file mode 100644 index 284779f..0000000 --- a/assignment2/smips/without_array.c +++ /dev/null @@ -1,337 +0,0 @@ -// Assignment 2 - spims.c -// Written by Rifa Jamal z5311190 -// COMP1521 20T2 - -#include -#include -#include -#include - -#define MAX_INSTRUCTION_CODES 1000 - -//functions -void decode_instruction(int32_t instruction); -int execute_instruction(int32_t instruction, int registers[]); -int check_register(int32_t instruction); -int get_register(int register_number); -int save_register(int register_number, int value); - - -int main(int arg, char *argv[]) { - - // check have command line argument - FILE *stream = fopen(argv[1], "r"); - - // check open succeeded - - int codes[MAX_INSTRUCTION_CODES]; - - //loop - if successfully read - //read from stream - hex into codes array - //i < check won't overflow the array - int n_instructions = 0; - while (n_instructions < MAX_INSTRUCTION_CODES - && fscanf(stream, "%x", &codes[n_instructions]) == 1) { - - n_instructions++; - } - - printf("Program\n"); - for (int j = 0; j < n_instructions; j++) { - //turn hex int mips code and print it - printf(" %d: ", j); - decode_instruction(codes[j]); - printf("\n"); - } - - printf("Output\n"); - int k = 0; - int *registers[32]; - int register_counter = 0; - while (k < n_instructions) { - - // Store registers into array - int value = check_register(codes[k]); - if (value != 0) { - registers[register_counter] = value; - register_counter++; - } - - //turn hex int mips code and print it - printf(" %d: ", k); - int instruction_line = execute_instruction(codes[k], registers); - printf("\n"); - - k = k + instruction_line; - } - - printf("Registers After Execution\n"); - int r_counter = 0; - while (r_counter < 32) { - if (registers[r_counter] != 0) { - printf("$%d = %d", r_counter, registers[r_counter]); - } - r_counter++; - } - -} -/* -int check_register(int32_t instruction) { - - // check if ori instruction - // ori $t, $s, I - uint32_t d = (instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (instruction >> 21) & 0x1F; // third 5 bits - - if (s == 0) { - return d; - } else { - return 0; - } -}*/ - -int get_register(int register_number) { - assert(register_number >= 0 && register_number < 32); - if (register_number == 0){ - registers[0] = 0; - return 0; - } - return registers[register_number]; -} - -int save_register(int register_number, int value) { - assert(register_number >= 0 && register_number < 32); - if (register_number != 0) { - registers[register_number] = value; - } -} - - -int execute_instruction(int32_t instruction, int registers[]) { - - uint32_t instruction_line = 0; - - //if ori and $0 , make t = I - //have other instructions - - // Find decimal values of different segments in instruction - - // Format Parts of R-type instruction code - uint32_t opcode = (instruction >> 26); // first 6 bits - uint32_t d = (instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (instruction >> 16) & 0x1F; // fourth 5 bits - uint32_t funct = instruction & 0x3F; // last 6 bits - - // Format Parts of J/I-type instruction code - uint32_t i = (instruction & 0xFFFF); // immediate address/ target - uint32_t result = 0; - - if (opcode == 0 && funct == 32) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - result = get_register(s) + get_register(t); - save_register(d, result); - - } else if (opcode == 0 && funct == 34) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - result = get_register(s) - get_register(t); - save_register(d, result); - - } else if (opcode == 0 && funct == 36) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - result = get_register(s) & get_register(t); - save_register(d, result); - - } else if (opcode == 0 && funct == 37) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - result = get_register(s) | get_register(t); - save_register(d, result); - - } else if (opcode == 0 && funct == 42) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - result = get_register(s) << get_register(t); - save_register(d, result); - - } else if (opcode == 28 && funct == 2) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - result = get_register(s) * get_register(t); - save_register(d, result); - - } else if (opcode == 4) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - if (get_register(s) == get_register(t)) { - (instruction_line) += i << 2; - } - - } else if (opcode == 5) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - if (get_register(s) != get_register(t)) { - (instruction_line) += i << 2; - } - - } else if (opcode == 8) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - result = get_register(s) + i; - save_register(t, result); - - } else if (opcode == 10) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - result = get_register(s) << i; - save_register(t, result); - - } else if (opcode == 12) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - result = get_register(s) & i; - save_register(t, result); - - } else if (opcode == 13) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - result = get_register(s) | i; - save_register(t, result); - - } else if (opcode == 15) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - result = i << 16; - save_register(t, result); - - } else if (opcode == 14) { - // xori $t, $s, I - // Bit Pattern: 001110ssssstttttIIIIIIIIIIIIIIII - result = get_register(s) ^ i; - save_register(t, result); - - } else if (opcode == 0 && funct == 0) { - // sll $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000000 - result = get_register(t) << i; - save_register(d, result); - - } else if (opcode == 0 && funct == 2) { - // srl $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000010 - result = get_register(t) >> i; - save_register(d, result); - - } else if (instruction == 12) { - // Bit Pattern: 00000000000000000000000000001100 - // syscall - - int v0; - int a0; - - // find v0 and a0 - v0 = registers[4]; - a0 = registers[2]; - - if (v0 == 1) { - // Print integer (in $a0 register) - printf("%d", a0); - } else if (v0 == 10) { - // Exit - return 1; - } else if (v0 == 11) { - // Print character - printf("%c", a0); - } - } - - return instruction_line; - -} - -void decode_instruction(int32_t instruction) { - - // Find decimal values of different segments in instruction - - // Format Parts of R-type instruction code - int opcode = (instruction >> 26); // first 6 bits - int d = (instruction >> 11) & 0x1F; // second 5 bits - int s = (instruction >> 21) & 0x1F; // third 5 bits - int t = (instruction >> 16) & 0x1F; // fourth 5 bits - int funct = instruction & 0x3F; // last 6 bits - - // Format Parts of J/I-type instruction code - int i = (instruction & 0xFFFF); // immediate address/ target - - if (opcode == 0 && funct == 32) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - printf("add $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 34) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - printf("sub $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 36) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - printf("and $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 37) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - printf("or $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 42) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - printf("slt $%d, $%d, $%d", d, s, t); - } else if (opcode == 28 && funct == 2) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - printf("mul $%d, $%d, $%d", d, s, t); - } else if (opcode == 4) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - printf("beq $%d, $%d, %d", s, t, i); - } else if (opcode == 5) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - printf("bne $%d, $%d, %d", s, t, i); - } else if (opcode == 8) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - printf("addi $%d, $%d, %d", t, s, i); - } else if (opcode == 10) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - printf("slti $%d, $%d, %d", t, s, i); - } else if (opcode == 12) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - printf("andi $%d, $%d, %d", t, s, i); - } else if (opcode == 13) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - printf("ori $%d, $%d, %d", t, s, i); - } else if (opcode == 15) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - printf("lui $%d, %d", t, i); - } else if (opcode == 14) { - // xori $t, $s, I - // Bit Pattern: 001110ssssstttttIIIIIIIIIIIIIIII - printf("xori $%d, $%d, %d", t, s, i); - } else if (opcode == 0 && funct == 0) { - // sll $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000000 - printf("sll $%d, $%d, %d", d, t, i); - } else if (opcode == 0 && funct == 2) { - // srl $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000010 - printf("srl $%d, $%d, %d", d, t, i); - } else if (instruction == 12) { - printf("syscall"); // Bit Pattern: 00000000000000000000000000001100 - } - -} \ No newline at end of file diff --git a/assignment2/smips/working.c b/assignment2/smips/working.c deleted file mode 100644 index b76aa2f..0000000 --- a/assignment2/smips/working.c +++ /dev/null @@ -1,791 +0,0 @@ -// Assignment 2 - spims.c -// Written by Rifa Jamal z5311190 -// COMP1521 20T2 - -#include -#include -#include -#include - -#define MAX_INSTRUCTION_CODES 1000 - -//functions -void decode_instruction(uint32_t instruction); -int execute_instruction(uint32_t instruction, uint32_t registers[32], int *k); - - -int main(int arg, char *argv[]) { - - // check have command line argument - FILE *stream = fopen(argv[1], "r"); - - // check open succeeded - - int codes[MAX_INSTRUCTION_CODES]; - uint32_t registers[32] = { 0 }; - - //loop - if successfully read - //read from stream - hex into codes array - //i < check won't overflow the array - int n_instructions = 0; - while (n_instructions < MAX_INSTRUCTION_CODES - && fscanf(stream, "%x", &codes[n_instructions]) == 1) { - - n_instructions++; - } - - printf("Program\n"); - for (int j = 0; j < n_instructions; j++) { - //turn hex int mips code and print it - if (j < 10 ) { - printf(" %d: ", j); - } else { - printf(" %d: ", j); - } - decode_instruction(codes[j]); - printf("\n"); - } - - printf("Output\n"); - int k = 0; - while (k < n_instructions) { - //turn hex int mips code and print it - if (execute_instruction(codes[k], registers, &k) != 0) { - break; - } - registers[0] = 0; - /* - int instruction_line = execute_instruction(codes[k], registers); - //printf("\n"); - if (instruction_line) { - k = k + instruction_line; - } else { - k++; - }*/ - k++; - } - - printf("Registers After Execution\n"); - int r_counter = 1; - while (r_counter < 32) { - if (registers[r_counter] != 0) { - printf("$%-2d = %d\n", r_counter, registers[r_counter]); - } - - r_counter++; - - } - -} - -int execute_instruction(uint32_t instruction, uint32_t registers[32], int *k) { - - //uint32_t instruction_line = 0; - - //if ori and $0 , make t = I - //have other instructions - - // Find decimal values of different segments in instruction - - // Format Parts of R-type instruction code - uint32_t opcode = (instruction >> 26); // first 6 bits - uint32_t d = (instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (instruction >> 16) & 0x1F; // fourth 5 bits - uint32_t funct = instruction & 0x3F; // last 6 bits - - // Format Parts of J/I-type instruction code - int16_t i = (instruction & 0xFFFF); // immediate address/ target - //uint32_t result = 0; - - if (opcode == 0 && funct == 32) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - registers[d] = registers[s] + registers[t]; - - } else if (opcode == 0 && funct == 34) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - registers[d] = registers[s] - registers[t]; - - } else if (opcode == 0 && funct == 36) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - registers[d] = registers[s] & registers[t]; - - } else if (opcode == 0 && funct == 37) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - registers[d] = registers[s] | registers[t]; - - } else if (opcode == 0 && funct == 42) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - registers[d] = registers[s] < registers[t]; - - } else if (opcode == 28 && funct == 2) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - registers[d] = registers[s] * registers[t]; - - } else if (opcode == 4) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - if (registers[s] == registers[t]) { - //(instruction_line) += i << 2; - //return (i << 2); - - //change i into 2's complement number - - //get top 16 bits of i - //uint32_t mask_top = (i >> 16); - //get sign of i - //uint32_t sign = i & 0x8000; - - /*if (sign == 1) { - mask_top = 0xFFFF; - i = i | (mask_top << 16); - }*/ - *k = *k + (i-1); - - } - - } else if (opcode == 5) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - if (registers[s] != registers[t]) { - //(instruction_line) += i << 2; - - ///change i into 2's complement number - - //get top 16 bits of i - /*uint32_t mask_top = (i >> 16); - //get sign of i - uint32_t sign = i & 0x8000; - - if (sign == 1) { - mask_top = 0xFFFF; - i = i | (mask_top << 16); - }*/ - *k = *k + (i-1); - } - - } else if (opcode == 8) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - //printf("%d %d", registers[t], registers[s]); - registers[t] = registers[s] + i; - - } else if (opcode == 10) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] < i; - - } else if (opcode == 12) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] & i; - - } else if (opcode == 13) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] | i; - - - } else if (opcode == 15) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - registers[t] = i << 16; - - } else if (opcode == 14) { - // xori $t, $s, I - // Bit Pattern: 001110ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] ^ i; - - } else if (opcode == 0 && funct == 0) { - // sll $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000000 - registers[d] = registers[t] << i; - - } else if (opcode == 0 && funct == 2) { - // srl $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000010 - registers[d] = registers[t] >> i; - - } else if (instruction == 12) { - // Bit Pattern: 00000000000000000000000000001100 - // syscall - - // find v0 and a0 - uint32_t v0 = registers[2]; - uint32_t a0 = registers[4]; - - if (v0 == 1) { - // Print integer (in $a0 register) - printf("%d", a0); - } else if (v0 == 10) { - // Exit - return 1; - } else if (v0 == 11) { - // Print character - printf("%c", a0); - } else { - printf("Unknown system call: %d\n", v0); - return 1; - } - } - - return 0; - -} - -void decode_instruction(uint32_t instruction) { - - // Find decimal values of different segments in instruction - - // Format Parts of R-type instruction code - int opcode = (instruction >> 26); // first 6 bits - int d = (instruction >> 11) & 0x1F; // second 5 bits - int s = (instruction >> 21) & 0x1F; // third 5 bits - int t = (instruction >> 16) & 0x1F; // fourth 5 bits - int funct = instruction & 0x3F; // last 6 bits - - // Format Parts of J/I-type instruction code - int16_t i = (instruction & 0xFFFF); // immediate address/ target - - if (opcode == 0 && funct == 32) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - printf("add $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 34) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - printf("sub $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 36) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - printf("and $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 37) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - printf("or $%d, $%d, $%d", d, s, t); - } else if (opcode == 0 && funct == 42) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - printf("slt $%d, $%d, $%d", d, s, t); - } else if (opcode == 28 && funct == 2) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - printf("mul $%d, $%d, $%d", d, s, t); - } else if (opcode == 4) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - printf("beq $%d, $%d, %d", s, t, i); - } else if (opcode == 5) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - printf("bne $%d, $%d, %d", s, t, i); - } else if (opcode == 8) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - printf("addi $%d, $%d, %d", t, s, i); - } else if (opcode == 10) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - printf("slti $%d, $%d, %d", t, s, i); - } else if (opcode == 12) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - printf("andi $%d, $%d, %d", t, s, i); - } else if (opcode == 13) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - printf("ori $%d, $%d, %d", t, s, i); - } else if (opcode == 15) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - printf("lui $%d, %d", t, i); - } else if (opcode == 14) { - // xori $t, $s, I - // Bit Pattern: 001110ssssstttttIIIIIIIIIIIIIIII - printf("xori $%d, $%d, %d", t, s, i); - } else if (opcode == 0 && funct == 0) { - // sll $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000000 - printf("sll $%d, $%d, %d", d, t, i); - } else if (opcode == 0 && funct == 2) { - // srl $d, $t, I - // Bit Pattern: 00000000000tttttdddddIIIII000010 - printf("srl $%d, $%d, %d", d, t, i); - } else if (instruction == 12) { - printf("syscall"); // Bit Pattern: 00000000000000000000000000001100 - } - -} - - - - - - - - - - - - - - - - - - - - - -/* - -// Assignment 2 - spims.c -// Written by Rifa Jamal z5311190 -// COMP1521 20T2 - -#include -#include -#include -#include - -#define MAX_INSTRUCTION_CODES 1000 -#define MAX_REGISTERS 32 - -// R-Type Instruction Defines -#define ADD 32 -#define SUB 34 -#define AND 36 -#define OR 37 -#define SLT 42 - -// Syscall Defines -#define SYSCALL 12 -#define REG_V0 2 -#define REG_A0 4 - -// I/J-Type Instruction Defines -#define MUL 28 -#define BEQ 4 -#define BNE 5 -#define ADDI 8 -#define SLTI 10 -#define ANDI 12 -#define ORI 13 -#define LUI 15 - -// Functions for Printing and Executing Mips Code -int instruction_start(uint32_t instruction); -void decode_r_type_instruction(uint32_t hex_instruction); -void decode_i_j_type_instruction(uint32_t hex_instruction); -//int execute_r_type_instruction(uint32_t hex_instruction, uint32_t registers[32]); -//int execute_i_j_type_instruction(uint32_t hex_instruction, uint32_t registers[32], int *k); -int execute_instruction(uint32_t instruction, uint32_t registers[32], int *k); - -// Main - -int main(int arg, char *argv[]) { - - // check have command line argument - FILE *stream = fopen(argv[1], "r"); - - // check open succeeded - - int codes[MAX_INSTRUCTION_CODES]; - uint32_t registers[32] = { 0 }; - - //loop - if successfully read - //read from stream - hex into codes array - //i < check won't overflow the array - int n_instructions = 0; - while (n_instructions < MAX_INSTRUCTION_CODES - && fscanf(stream, "%x", &codes[n_instructions]) == 1) { - - n_instructions++; - } - - printf("Program\n"); - for (int j = 0; j < n_instructions; j++) { - //turn hex int mips code and print it - if (j < 10 ) { - printf(" %d: ", j); - } else { - printf(" %d: ", j); - } - - if (instruction_start(codes[j]) == 0) { - decode_r_type_instruction(codes[j]); - } else { - decode_i_j_type_instruction(codes[j]); - } - - printf("\n"); - } - - */ - - /* - printf("Output\n"); - int k = 0; - while (k < n_instructions) { - //turn hex int mips code and print it - if (execute_instruction(codes[k], registers, &k) != 0) { - break; - } - registers[0] = 0; - k++; - - /* - if (instruction_start(codes[k] == 0)) { - if (execute_r_type_instruction(codes[k], registers) != 0) { - break; - } - } else { - execute_i_j_type_instruction(codes[k], registers, &k); - } - - registers[0] = 0; - k++; - */ - - // } - - - - - /* - printf("Registers After Execution\n"); - int r_counter = 1; - while (r_counter < 32) { - if (registers[r_counter] != 0) { - printf("$%-2d = %d\n", r_counter, registers[r_counter]); - } - - r_counter++; - - } - -} - -int instruction_start(uint32_t instruction) { - uint32_t opcode = (instruction >> 26); // first 6 bits - return opcode; -} - -int execute_instruction(uint32_t instruction, uint32_t registers[32], int *k) { - - uint32_t instruction_start = (instruction >> 26); // first 6 bits - uint32_t d = (instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (instruction >> 16) & 0x1F; // fourth 5 bits - uint32_t instruction_end = instruction & 0x3F; // last 6 bits - int16_t i = (instruction & 0xFFFF); // immediate address/ target - - if (instruction_end == ADD) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - registers[d] = registers[s] + registers[t]; - - } else if (instruction_end == SUB) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - registers[d] = registers[s] - registers[t]; - - } else if (instruction_end == AND) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - registers[d] = registers[s] & registers[t]; - - } else if (instruction_end == OR) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - registers[d] = registers[s] | registers[t]; - - } else if (instruction_end == SLT) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - registers[d] = registers[s] < registers[t]; - - } else if (instruction_start == MUL) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - registers[d] = registers[s] * registers[t]; - - } else if (instruction_start == BEQ) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - if (registers[s] == registers[t]) { - *k = *k + (i-1); - - } - - } else if (instruction_start == BNE) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - if (registers[s] != registers[t]) { - - *k = *k + (i-1); - } - - } else if (instruction_start == ADDI) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - //printf("%d %d", registers[t], registers[s]); - registers[t] = registers[s] + i; - - } else if (instruction_start == SLTI) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] < i; - - } else if (instruction_start == ANDI) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] & i; - - } else if (instruction_start == ORI) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] | i; - - - } else if (instruction_start == LUI) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - registers[t] = i << 16; - - } else if (instruction == SYSCALL) { - // Bit Pattern: 00000000000000000000000000001100 - // syscall - - // find v0 and a0 - uint32_t v0 = registers[REG_V0]; - uint32_t a0 = registers[REG_A0]; - - if (v0 == 1) { - // Print integer (in $a0 register) - printf("%d", a0); - } else if (v0 == 10) { - // Exit - return 1; - } else if (v0 == 11) { - // Print character - printf("%c", a0); - } else { - printf("Unknown system call: %d\n", v0); - return 1; - } - } - - return 0; - -} - -*/ - -/* -int execute_r_type_instruction(uint32_t hex_instruction, uint32_t registers[32]) { - - // Format Parts of R-type instruction code - uint32_t d = (hex_instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (hex_instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (hex_instruction >> 16) & 0x1F; // fourth 5 bits - uint32_t instruction = hex_instruction & 0x3F; // last 6 bits - - if (instruction == ADD) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - registers[d] = registers[s] + registers[t]; - - } else if (instruction == SUB) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - registers[d] = registers[s] - registers[t]; - - } else if (instruction == AND) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - registers[d] = registers[s] & registers[t]; - - } else if (instruction == OR) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - registers[d] = registers[s] | registers[t]; - - } else if (instruction == SLT) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - registers[d] = registers[s] < registers[t]; - - } else if (instruction == SYSCALL) { - // syscall - // Bit Pattern: 00000000000000000000000000001100 - - // find registers v0 and a0 - uint32_t reg_v0 = registers[REG_V0]; - uint32_t reg_a0 = registers[REG_A0]; - - if (reg_v0 == 1) { - // Print integer (in $a0 register) - printf("%d", reg_a0); - } else if (reg_v0 == 10) { - // Exit - return 1; - } else if (reg_v0 == 11) { - // Print character - printf("%c", reg_a0); - } else { - printf("Unknown system call: %d\n", reg_v0); - return 1; - } - } - - return 0; -} - -int execute_i_j_type_instruction(uint32_t hex_instruction, uint32_t registers[32], int *k) { - - uint32_t instruction = (hex_instruction >> 26); // first 6 bits - uint32_t d = (hex_instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (hex_instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (hex_instruction >> 16) & 0x1F; // fourth 5 bits - int16_t i = (hex_instruction & 0xFFFF); // immediate address/ target - - if (instruction == MUL) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - registers[d] = registers[s] * registers[t]; - - } else if (instruction == BEQ) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - if (registers[s] == registers[t]) { - *k = *k + (i-1); - } - - } else if (instruction == BNE) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - if (registers[s] != registers[t]) { - *k = *k + (i-1); - } - - } else if (instruction == ADDI) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - //printf("%d %d", registers[t], registers[s]); - registers[t] = registers[s] + i; - - } else if (instruction == SLTI) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] < i; - - } else if (instruction == ANDI) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] & i; - - } else if (instruction == ORI) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - registers[t] = registers[s] | i; - - } else if (instruction == LUI) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - registers[t] = i << 16; - } - - return 0; -} -*/ - -/* - -void decode_r_type_instruction(uint32_t hex_instruction) { - - // Format Parts of R-type instruction code - uint32_t d = (hex_instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (hex_instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (hex_instruction >> 16) & 0x1F; // fourth 5 bits - uint32_t instruction = hex_instruction & 0x3F; // last 6 bits - - if (instruction == ADD) { - // add $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100000 - printf("add $%d, $%d, $%d", d, s, t); - } else if (instruction == SUB) { - // sub $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100010 - printf("sub $%d, $%d, $%d", d, s, t); - } else if (instruction == AND) { - // and $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100100 - printf("and $%d, $%d, $%d", d, s, t); - } else if (instruction == OR) { - // or $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000100101 - printf("or $%d, $%d, $%d", d, s, t); - } else if (instruction == SLT) { - // slt $d, $s, $t - // Bit Pattern: 000000ssssstttttddddd00000101010 - printf("slt $%d, $%d, $%d", d, s, t); - } else if (instruction == SYSCALL) { - // Bit Pattern: 00000000000000000000000000001100 - printf("syscall"); - } -} - -void decode_i_j_type_instruction(uint32_t hex_instruction) { - - uint32_t instruction = (hex_instruction >> 26); // first 6 bits - uint32_t d = (hex_instruction >> 11) & 0x1F; // second 5 bits - uint32_t s = (hex_instruction >> 21) & 0x1F; // third 5 bits - uint32_t t = (hex_instruction >> 16) & 0x1F; // fourth 5 bits - int16_t i = (hex_instruction & 0xFFFF); // immediate address/ target - - if (instruction == MUL) { - // mul $d, $s, $t - // Bit Pattern: 011100ssssstttttddddd00000000010 - printf("mul $%d, $%d, $%d", d, s, t); - } else if (instruction == BEQ) { - // beq $s, $t, I - // Bit Pattern: 000100ssssstttttIIIIIIIIIIIIIIII - printf("beq $%d, $%d, %d", s, t, i); - } else if (instruction == BNE) { - // bne $s, $t, I - // Bit Pattern: 000101ssssstttttIIIIIIIIIIIIIIII - printf("bne $%d, $%d, %d", s, t, i); - } else if (instruction == ADDI) { - // addi $t, $s, I - // Bit Pattern: 001000ssssstttttIIIIIIIIIIIIIIII - printf("addi $%d, $%d, %d", t, s, i); - } else if (instruction == SLTI) { - // slti $t, $s, I - // Bit Pattern: 001010ssssstttttIIIIIIIIIIIIIIII - printf("slti $%d, $%d, %d", t, s, i); - } else if (instruction == ANDI) { - // andi $t, $s, I - // Bit Pattern: 001100ssssstttttIIIIIIIIIIIIIIII - printf("andi $%d, $%d, %d", t, s, i); - } else if (instruction == ORI) { - // ori $t, $s, I - // Bit Pattern: 001101ssssstttttIIIIIIIIIIIIIIII - printf("ori $%d, $%d, %d", t, s, i); - } else if (instruction == LUI) { - // lui $t, I - // Bit Pattern: 00111100000tttttIIIIIIIIIIIIIIII - printf("lui $%d, %d", t, i); - } - -} - - -*/ - diff --git a/lab02/bcd b/lab02/bcd deleted file mode 100755 index 357be1f..0000000 Binary files a/lab02/bcd and /dev/null differ diff --git a/lab02/bcd.c b/lab02/bcd.c deleted file mode 100644 index 3fdbe57..0000000 --- a/lab02/bcd.c +++ /dev/null @@ -1,167 +0,0 @@ -#include -#include -#include -#include - -int bcd(int bcd_value); - -int main(int argc, char *argv[]) { - - for (int arg = 1; arg < argc; arg++) { - long l = strtol(argv[arg], NULL, 0); - assert(l >= 0 && l <= 0x0909); - int bcd_value = l; - - printf("%d\n", bcd(bcd_value)); - } - - return 0; -} - -// given a BCD encoded value between 0 .. 99 -// return corresponding integer -int bcd(int bcd_value) { - -//////////////// MEHTOD 1 ///////////////////////////////// - - /* - - uint16_t mask = 32768; // 0b 1000 0000 0000 0000 - // split of 16 bits into 2 bytes - for 2 decimal digits - int result_one[8]; - int result_two[8]; - - //digit 1 - for (int i = 0; i < 8; i++) { - if ((mask & bcd_value) == 0) { - result_one[i] = 0; - } else { - result_one[i] = 1; - } - mask >>= 1; - } - - //digit 2 - for (int j = 0; j < 8; j++) { - if ((mask & bcd_value) == 0) { - result_two[j] = 0; - } else { - result_two[j] = 1; - } - mask >>= 1; - } - - //convert two digit arrays back into decimal - - uint8_t digit_one = 0; - uint8_t mask_one = 1; - - for (int pos_one = 7; mask_one; pos_one--) { - if (result_one[pos_one] == 1) { - // | inclusive OR - digit_one |= mask_one; - } - mask_one <<= 1; - } - - uint8_t digit_two = 0; - uint8_t mask_two = 1; - - for (int pos_two = 7; mask_two; pos_two--) { - if (result_two[pos_two] == 1) { - // | inclusive OR - digit_two |= mask_two; - } - mask_two <<= 1; - } - - //return digits - - int decimal = digit_two + (10 * digit_one) ; - - //decimal[0] = digit_one; - //decimal[1] = digit_two; - - return decimal; - - - */ - - ////////////// MEHTOD 2 ////////////////////////// - - uint16_t decimal = 0; - - int i = 1; - - for (int j = 0; j < 16; j += 8) { - decimal = decimal + (i * (bcd_value >> j) & 0x00FF); - i = i * 10; - } - - return decimal; - - /* - - // first convert integer to binary - - uint16_t mask = 1; - int buffer[16]; - - for (int i = 15; mask; i--) { - if((bcd_value & mask) == 0) { - buffer[i] = 0; - } else { - buffer[i] = 1; - } - mask <<= 1; - } - - // split the 16 bits into 2 * 8 bits - // copy into two different strings - - int byte_one[8]; - int byte_two[8]; - - for (int j = 0; j < 8; j++) { - byte_one[j] = buffer[j]; - } - - for (int j = 8; j < 16; j++) { - for (int k = 0; k < 8; k++){ - byte_two[k] = buffer[j]; - } - } - - // now convert these two bytes into decimal - - uint16_t result_one = 0; - uint16_t mask_one = 1; - - for (int pos_one = 7; mask_one; pos_one--) { - if (byte_one[pos_one] == 1) { - // | inclusive OR - result_one |= mask_one; - } - mask_one <<= 1; - } - - uint16_t result_two = 0; - uint16_t mask_two = 1; - - for (int pos_two = 7; mask_two; pos_two--) { - if (byte_two[pos_two] == 1) { - // | inclusive OR - result_two |= mask_two; - } - mask_two <<= 1; - } - - return result_one + result_two; - - */ - -} - - - - diff --git a/lab02/bcd_add.c b/lab02/bcd_add.c deleted file mode 100644 index c844d84..0000000 --- a/lab02/bcd_add.c +++ /dev/null @@ -1,144 +0,0 @@ -#include -#include -#include -#include -#include - -// -// Store an arbitray length Binary Coded Decimal number. -// bcd points to an array of size n_bcd -// Each array element contains 1 decimal digit. -// Digits are stored in reverse order. -// -// For example if 42 is stored then -// n_bcd == 2 -// bcd[0] == 0x02 -// bcd[1] == 0x04 -// - -typedef struct big_bcd { - unsigned char *bcd; - int n_bcd; -} big_bcd_t; - - -big_bcd_t *bcd_add(big_bcd_t *x, big_bcd_t *y); -void bcd_print(big_bcd_t *number); -void bcd_free(big_bcd_t *number); -big_bcd_t *bcd_from_string(char *string); - -int main(int argc, char *argv[]) { - if (argc != 3) { - fprintf(stderr, "Usage: %s \n", argv[0]); - exit(1); - } - - big_bcd_t *left = bcd_from_string(argv[1]); - big_bcd_t *right = bcd_from_string(argv[2]); - - big_bcd_t *result = bcd_add(left, right); - - bcd_print(result); - printf("\n"); - - bcd_free(left); - bcd_free(right); - bcd_free(result); - - return 0; -} - - -// DO NOT CHANGE THE CODE ABOVE HERE - - - -big_bcd_t *bcd_add(big_bcd_t *x, big_bcd_t *y) { - - // PUT YOUR CODE HERE - - // SAMPLE SOLUTION - - big_bcd_t *sum = malloc(sizeof *sum); - assert(sum); - - int n_digits = 0; - if (x->n_bcd > y->n_bcd) { - n_digits = x->n_bcd; - } else { - n_digits = y->n_bcd; - } - - sum->n_bcd = n_digits; - sum->bcd = malloc(n_digits * sizeof sum->bcd[0]); - assert(sum->bcd); - - int carry = 0; - for (size_t i = 0; i < n_digits; i++) { - int digit_sum = carry; - - if (i < x->n_bcd) { - digit_sum += x->bcd[i]; - } - - if (i < y->n_bcd) { - digit_sum += y->bcd[i]; - } - - sum->bcd[i] = digit_sum % 10; - carry = digit_sum / 10; - } - - if (carry) { - // we need to grow the array by to fit the carry digit - sum->n_bcd = n_digits + 1; - sum->bcd = realloc(sum->bcd, (n_digits + 1) * sizeof sum->bcd[0]); - assert(sum->bcd); - sum->bcd[n_digits] = carry; - } - - return sum; -} -} - - -// DO NOT CHANGE THE CODE BELOW HERE - - -// print a big_bcd_t number -void bcd_print(big_bcd_t *number) { - // if you get an error here your bcd_add is returning an invalid big_bcd_t - assert(number->n_bcd > 0); - for (int i = number->n_bcd - 1; i >= 0; i--) { - putchar(number->bcd[i] + '0'); - } -} - -// free storage for big_bcd_t number -void bcd_free(big_bcd_t *number) { - // if you get an error here your bcd_add is returning an invalid big_bcd_t - // or it is calling free for the numbers it is given - free(number->bcd); - free(number); -} - -// convert a string to a big_bcd_t number -big_bcd_t *bcd_from_string(char *string) { - big_bcd_t *number = malloc(sizeof *number); - assert(number); - - int n_digits = strlen(string); - assert(n_digits); - number->n_bcd = n_digits; - - number->bcd = malloc(n_digits * sizeof number->bcd[0]); - assert(number->bcd); - - for (int i = 0; i < n_digits; i++) { - int digit = string[n_digits - i - 1]; - assert(isdigit(digit)); - number->bcd[i] = digit - '0'; - } - - return number; -} diff --git a/lab02/bcd_arithmetic.c b/lab02/bcd_arithmetic.c deleted file mode 100644 index 8fa7602..0000000 --- a/lab02/bcd_arithmetic.c +++ /dev/null @@ -1,171 +0,0 @@ -#include -#include -#include -#include -#include - -// -// Store an arbitray length Binary Coded Decimal number -// bcd points to an array of size n_bcd -// each array element contains 1 decimal digit -// - -typedef struct big_bcd { - unsigned char *bcd; - int n_bcd; -} big_bcd_t; - - -void bcd_print(big_bcd_t *number); -void bcd_free(big_bcd_t *number); -big_bcd_t *bcd_from_string(char *string); - -big_bcd_t *expression(char ***tokens); -big_bcd_t *term(char ***tokens); - -int main(int argc, char *argv[]) { - char **tokens = argv + 1; - - // tokens points in turn to each of the elements of argv - // as the expression is evaluated. - - if (*tokens) { - big_bcd_t *result = expression(&tokens); - bcd_print(result); - printf("\n"); - bcd_free(result); - } - - return 0; -} - - -// DO NOT CHANGE THE CODE ABOVE HERE - - -big_bcd_t *bcd_add(big_bcd_t *x, big_bcd_t *y) { - // PUT YOUR CODE HERE - return NULL; -} - -big_bcd_t *bcd_subtract(big_bcd_t *x, big_bcd_t *y) { - // PUT YOUR CODE HERE - return NULL; -} - -big_bcd_t *bcd_multiply(big_bcd_t *x, big_bcd_t *y) { - // PUT YOUR CODE HERE - return NULL; -} - -big_bcd_t *bcd_divide(big_bcd_t *x, big_bcd_t *y) { - // PUT YOUR CODE HERE - return NULL; -} - - -// DO NOT CHANGE THE CODE BELOW HERE - - -// print a big_bcd_t number -void bcd_print(big_bcd_t *number) { - // if you get an error here your bcd_arithmetic is returning an invalid big_bcd_t - assert(number->n_bcd > 0); - for (int i = number->n_bcd - 1; i >= 0; i--) { - putchar(number->bcd[i] + '0'); - } -} - - -// DO NOT CHANGE THE CODE BELOW HERE - -// free storage for big_bcd_t number -void bcd_free(big_bcd_t *number) { - // if you get an error here your bcd_arithmetic is returning an invalid big_bcd_t - // or it is calling free for the numbers it is given - free(number->bcd); - free(number); -} - -// convert a string to a big_bcd_t number -big_bcd_t *bcd_from_string(char *string) { - big_bcd_t *number = malloc(sizeof *number); - assert(number); - - int n_digits = strlen(string); - assert(n_digits); - number->n_bcd = n_digits; - - number->bcd = malloc(n_digits * sizeof number->bcd[0]); - assert(number->bcd); - - for (int i = 0; i < n_digits; i++) { - int digit = string[n_digits - i - 1]; - assert(isdigit(digit)); - number->bcd[i] = digit - '0'; - } - - return number; -} - - -// simple recursive descent evaluator for big_bcd_t expressions -big_bcd_t *expression(char ***tokens) { - - big_bcd_t *left = term(tokens); - assert(left); - - if (!**tokens|| (***tokens != '+' && ***tokens != '-')) { - return left; - } - - char *operator = **tokens; - (*tokens)++; - - big_bcd_t *right = expression(tokens); - assert(right); - - big_bcd_t *result; - if (operator[0] == '+') { - result = bcd_add(left, right); - } else { - assert(operator[0] == '-'); - result = bcd_subtract(left, right); - } - assert(result); - - bcd_free(left); - bcd_free(right); - return result; -} - - -// evaluate a term of a big_bcd_t expression -big_bcd_t *term(char ***tokens) { - - big_bcd_t *left = bcd_from_string(**tokens); - assert(left); - (*tokens)++; - - if (!**tokens || (***tokens != '*' && ***tokens != '/')) { - return left; - } - - char *operator = **tokens; - (*tokens)++; - - big_bcd_t *right = term(tokens); - assert(right); - - big_bcd_t *result; - if (operator[0] == '*') { - result = bcd_multiply(left, right); - } else { - result = bcd_divide(left, right); - } - assert(result); - - bcd_free(left); - bcd_free(right); - return result; -} diff --git a/lab02/packed_bcd b/lab02/packed_bcd deleted file mode 100755 index ba1aea5..0000000 Binary files a/lab02/packed_bcd and /dev/null differ diff --git a/lab02/packed_bcd.c b/lab02/packed_bcd.c deleted file mode 100644 index e2a8f18..0000000 --- a/lab02/packed_bcd.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include -#include - -#define N_BCD_DIGITS 8 - -uint32_t packed_bcd(uint32_t packed_bcd); -int change_to_4_bits(int packed_bcd_value, uint32_t mask); -int convert_to_decimal(int digit); - -int main(int argc, char *argv[]) { - - for (int arg = 1; arg < argc; arg++) { - long l = strtol(argv[arg], NULL, 0); - assert(l >= 0 && l <= UINT32_MAX); - uint32_t packed_bcd_value = l; - - printf("%lu\n", (unsigned long)packed_bcd(packed_bcd_value)); - } - - return 0; -} - -// given a packed BCD encoded value between 0 .. 99999999 -// return the corresponding integer -uint32_t packed_bcd(uint32_t packed_bcd_value) { - - uint32_t decimal = 0; - - int i = 1; - - for (int j = 0; j < 32; j += 4) { - decimal = decimal + (i * ((packed_bcd_value >> j) & 0x0000F)); - i = i * 10; - } - - return decimal; - -} diff --git a/lab02/sixteen_in b/lab02/sixteen_in deleted file mode 100755 index 6d3534a..0000000 Binary files a/lab02/sixteen_in and /dev/null differ diff --git a/lab02/sixteen_in.c b/lab02/sixteen_in.c deleted file mode 100644 index 253bbf8..0000000 --- a/lab02/sixteen_in.c +++ /dev/null @@ -1,111 +0,0 @@ -// -// Sample solution for COMP1521 lab exercises -// -// Convert string of binary digits to 16-bit signed integer - -#include -#include -#include -#include - -#define N_BITS 16 - -int16_t sixteen_in(char *bits); - -int main(int argc, char *argv[]) { - - for (int arg = 1; arg < argc; arg++) { - printf("%d\n", sixteen_in(argv[arg])); - } - - return 0; -} - -// -// given a string of binary digits ('1' and '0') -// return the corresponding signed 16 bit integer -// - -// My explantion on how this works: - -//say char[bits] = 0110 -//pos = 0, bits[0] = 1, ascii = '0' - '0' = 0 -//value = 0000 << 1 = 0000, value = 0000 | 0 = 0000 - -//pos = 1, bits[1] = 1, ascii = '1' - '0' = 1 -//value = 0000 << 1 = 0000, value = 0000 | 0001 = 0001 - -//pos = 2, bits[2] = 1, ascii = 1 -//value = 0001 << 1 = 0010, value = 0010 | 1 = 0010 | 0001 = 0011 - -//pos = 3, bits[3] = 0, ascii = 0 -//value = 0011 << 1 = 0110, value = 0110 | 0000 = 0110 - -//Hence, we arrived at original char and computer will convert to decimal - -int16_t sixteen_in(char *bits) { - -////////////////// METHOD ONE ////////////////////////////////////////// - - - //note: value starts off as 16 0's - uint16_t value = 0; - - //loop through each bit individually - //copy each character from the string onto individual bits of 'value' - //note: at the end computer will manually change bits into decimal. - for (int position = 0; position < N_BITS; position++) { - int ascii_binary_digit = bits[position]; - //note: we are changing ascii value of digit to its corresponding value - //e.g. 1 in ascii is 49, so '1' - '0' = 49 - 48 = 1 - int binary_digit = ascii_binary_digit - '0'; - - //move to the next digit to next number can be copied in - //e.g. if value = 0001, value then becomes 0010 - value = value << 1; - - //OR operator results in 1 unless both bits are 0, - //so whenever there is a 1, value will also be 1 - //e.g. if value is 0010 and binary digit is 1 (0001) - //then 0010 | 0001 = 0011 - value = value | binary_digit; - - } - return value; - - -////////////////// METHOD TWO ////////////////////////////////////// - -/* - -uint16_t result = 0; - -//use mask to look/extract single bit at a time from bits -uint16_t mask = 1; - -// mask: 0b 0000 0000 0000 0001 -// bits: 0b 0000 0101 0110 0111 (example of bits) -// | -// result: 0b 0000 0101 0110 0111 - -//this tells use where the '1' is -//we can move the 1 in mask left one position, we can look at each position in bits individually -//move mask with << - -//same as saying while (mask != 0) i.e. when you move << all the way you get 0 -//check for each character in string of digits -// i -- since checking bits from right to left (as mask moves) -for (int i = 15; mask; i--) { - if (bits[i] == '1') { - // | inclusive OR - result |= mask; - } - mask <<= 1; -} - -return result; - -*/ - -} - diff --git a/lab02/sixteen_out b/lab02/sixteen_out deleted file mode 100755 index 45be9fb..0000000 Binary files a/lab02/sixteen_out and /dev/null differ diff --git a/lab02/sixteen_out.c b/lab02/sixteen_out.c deleted file mode 100644 index c212648..0000000 --- a/lab02/sixteen_out.c +++ /dev/null @@ -1,57 +0,0 @@ -// -// Sample solution for COMP1521 lab exercises -// -// Convert a 16-bit signed integer to a string of binary digits - -#include -#include -#include -#include - -#define N_BITS 16 - -char *sixteen_out(int16_t value); - -int main(int argc, char *argv[]) { - - for (int arg = 1; arg < argc; arg++) { - long l = strtol(argv[arg], NULL, 0); - assert(l >= INT16_MIN && l <= INT16_MAX); - int16_t value = l; - - char *bits = sixteen_out(value); - printf("%s\n", bits); - - free(bits); - } - - return 0; -} - -// given a signed 16 bit integer -// return a null-terminated string of 16 binary digits ('1' and '0') -// storage for string is allocated using malloc -char *sixteen_out(int16_t value) { - - //allocate memory to a string to hold binary digits + a terminating 0 - - //int16_t binary_digits = 8 * sizeof(value); - //char *string = malloc(binary_digits + 1); - - char *buffer = malloc((16 + 1) * sizeof(char)); - buffer[16] = '\0'; - uint16_t mask = 1; - - //print digits - for (int i = 15; mask; i--) { - if((value & mask) == 0) { - buffer[i] = '0'; - } else { - buffer[i] = '1'; - } - mask <<= 1; - } - return buffer; - -} - diff --git a/lab03/Makefile b/lab03/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/lab03/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/lab03/float_2048 b/lab03/float_2048 deleted file mode 100755 index ba48c8c..0000000 Binary files a/lab03/float_2048 and /dev/null differ diff --git a/lab03/float_2048.c b/lab03/float_2048.c deleted file mode 100644 index ccc1e30..0000000 --- a/lab03/float_2048.c +++ /dev/null @@ -1,156 +0,0 @@ -// Multiply a float by 2048 using bit operations only - -#include -#include -#include -#include - -#include "floats.h" - -uint32_t componenents_to_float (float_components_t f); -float_components_t float_bits(uint32_t f); -int is_nan(float_components_t f); -int is_positive_infinity(float_components_t f); -int is_negative_infinity(float_components_t f); -int is_zero(float_components_t f); - -// float_2048 is given the bits of a float f as a uint32_t -// it uses bit operations and + to calculate f * 2048 -// and returns the bits of this value as a uint32_t -// -// if the result is too large to be represented as a float +inf or -inf is returned -// -// if f is +0, -0, +inf or -int, or Nan it is returned unchanged -// -// float_2048 assumes f is not a denormal number -// -uint32_t float_2048(uint32_t f) { - // PUT YOUR CODE HERE - - // note 2048 = 2^11 - // float = sign * (1 + frac) * 2^(exp - 127) * 2^(11) - //simplifies to: sign * (1 + frac) * 2^(exp - 127 + 11) - //so we are only modifying the exponent - - float_components_t fc = float_bits(f); - - if (is_nan(fc) == 1 - || is_negative_infinity(fc) == 1 - || is_positive_infinity(fc) == 1 - || is_zero(fc) == 1) { - return f; - } else { - fc.exponent += 11; - - //check if number too large - if (fc.exponent >= 0xff) { - //note you can't return '-/inf' - // so change components - fc.fraction = 0; - fc.exponent = 0xff; - } - - return componenents_to_float (fc); - } - - -} - -// join componenents of float back into a new float -// including new exponent - -uint32_t componenents_to_float (float_components_t f) { - - uint32_t new_float = 0; - - //shift sign to beginning i.e. move 31 left - // shift exponenet to next 8 bits (after sign !!) - // keep fraction where it is. - new_float = f.sign << 31 | new_float; - new_float = f.exponent << 23 | new_float; - new_float = f.fraction << 0 | new_float; - - return new_float; - -} - -// separate out the 3 components of a float -float_components_t float_bits(uint32_t f) { - // PUT YOUR CODE HERE - // components of struct in float.h - float_components_t fc; - fc.sign = (f >> 31) & 0x1; - fc.exponent = (f >> 23) & 0xff; - fc.fraction = f & 0x7fffff; - - return fc; -} - -// given the 3 components of a float -// return 1 if it is NaN, 0 otherwise -int is_nan(float_components_t f) { - // PUT YOUR CODE HERE - - //NaN means: - //sign = either 0 or 1. - //biased exponent = all 1 bits. - //fraction = anything except all 0 bits (since all 0 bits represents infinity). - - if ((f.exponent == 0xff) && f.fraction != 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is inf, 0 otherwise -int is_positive_infinity(float_components_t f) { - // PUT YOUR CODE HERE - - //positive infinity means: - //sign = 0 for positive infinity - //biased exponent = all 1 bits. - //fraction = all 0 bits. - - if (f.sign == 0 && f.exponent == 0xff && f.fraction == 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is -inf, 0 otherwise -int is_negative_infinity(float_components_t f) { - // PUT YOUR CODE HERE - - //negative infinity means: - //sign = 1 for negative infinity - //biased exponent = all 1 bits. - //fraction = all 0 bits. - - if (f.sign == 1 && f.exponent == 0xff && f.fraction == 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is 0 or -0, 0 otherwise -int is_zero(float_components_t f) { - // PUT YOUR CODE HERE - - // if the float is zero, then: - //sign = 0 for positive zero, 1 for negative zero. - //biased exponent = 0. - //fraction = 0. - - if (f.exponent == 0 && f.fraction == 0) { - return 1; - } else { - return 0; - } - -} diff --git a/lab03/float_2048.mk b/lab03/float_2048.mk deleted file mode 120000 index c49679e..0000000 --- a/lab03/float_2048.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_2048/files.ln/float_2048.mk \ No newline at end of file diff --git a/lab03/float_bits b/lab03/float_bits deleted file mode 100755 index ef2b50d..0000000 Binary files a/lab03/float_bits and /dev/null differ diff --git a/lab03/float_bits.c b/lab03/float_bits.c deleted file mode 100644 index cdc4a6d..0000000 --- a/lab03/float_bits.c +++ /dev/null @@ -1,89 +0,0 @@ -// Extract the 3 parts of a float using bit operations only - -#include -#include -#include -#include - -#include "floats.h" - -// separate out the 3 components of a float -float_components_t float_bits(uint32_t f) { - // PUT YOUR CODE HERE - // components of struct in float.h - float_components_t fc; - fc.sign = (f >> 31) & 0x1; - fc.exponent = (f >> 23) & 0xff; - fc.fraction = f & 0x7fffff; - - return fc; -} - -// given the 3 components of a float -// return 1 if it is NaN, 0 otherwise -int is_nan(float_components_t f) { - // PUT YOUR CODE HERE - - //NaN means: - //sign = either 0 or 1. - //biased exponent = all 1 bits. - //fraction = anything except all 0 bits (since all 0 bits represents infinity). - - if ((f.exponent == 0xff) && f.fraction != 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is inf, 0 otherwise -int is_positive_infinity(float_components_t f) { - // PUT YOUR CODE HERE - - //positive infinity means: - //sign = 0 for positive infinity - //biased exponent = all 1 bits. - //fraction = all 0 bits. - - if (f.sign == 0 && f.exponent == 0xff && f.fraction == 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is -inf, 0 otherwise -int is_negative_infinity(float_components_t f) { - // PUT YOUR CODE HERE - - //negative infinity means: - //sign = 1 for negative infinity - //biased exponent = all 1 bits. - //fraction = all 0 bits. - - if (f.sign == 1 && f.exponent == 0xff && f.fraction == 0) { - return 1; - } else { - return 0; - } -} - -// given the 3 components of a float -// return 1 if it is 0 or -0, 0 otherwise -int is_zero(float_components_t f) { - // PUT YOUR CODE HERE - - // if the float is zero, then: - //sign = 0 for positive zero, 1 for negative zero. - //biased exponent = 0. - //fraction = 0. - - if (f.exponent == 0 && f.fraction == 0) { - return 1; - } else { - return 0; - } - -} diff --git a/lab03/float_bits.mk b/lab03/float_bits.mk deleted file mode 120000 index 1cb82dd..0000000 --- a/lab03/float_bits.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_bits/files.ln/float_bits.mk \ No newline at end of file diff --git a/lab03/float_less b/lab03/float_less deleted file mode 100755 index 2afe10c..0000000 Binary files a/lab03/float_less and /dev/null differ diff --git a/lab03/float_less.c b/lab03/float_less.c deleted file mode 100644 index 79c4dc7..0000000 --- a/lab03/float_less.c +++ /dev/null @@ -1,108 +0,0 @@ -// Compare 2 floats using bit operations only - -#include -#include -#include - -#include "floats.h" - -float_components_t float_to_components(uint32_t f); -uint32_t float_from_components(float_components_t f); - -// float_less is given the bits of 2 floats bits1, bits2 as a uint32_t -// and returns 1 if bits1 < bits2, 0 otherwise -// 0 is return if bits1 or bits2 is Nan -// only bit operations and integer comparisons are used -uint32_t float_less(uint32_t bits1, uint32_t bits2) { - // PUT YOUR CODE HERE - - // SAMPLE SOLUTIONS - - float_components_t f1 = float_to_components(bits1); - float_components_t f2 = float_to_components(bits2); - - if (is_nan(f1) || is_nan(f2)) { - return 0; - } - - if (is_negative_infinity(f1)) { - // f1 is -inf, return 1 if f2 is not -inf - return !is_negative_infinity(f2); - } - - if (is_positive_infinity(f1) || is_negative_infinity(f2)) { - return 0; - } - - if (is_positive_infinity(f2)) { - return 1; - } - - if (is_zero(f1) && is_zero(f2)) { - // special needed to handle +0, -0 - return 0; - } - - if (f1.sign != f2.sign) { - // f1, f2 have different signs, return 1 if f1 negative - return f1.sign; - } - - if (f1.exponent != f2.exponent) { - // f1, f2 have different exponents - if (f1.sign) { - return f2.exponent < f1.exponent; - } else { - return f1.exponent < f2.exponent; - } - } - - // exponent and sign are identical - if (f1.sign) { - return f2.fraction < f1.fraction; - } else { - return f1.fraction < f2.fraction; - } -} - -#define SIGN_SHIFT 31 -#define SIGN_MASK 0x1 -#define EXPONENT_SHIFT 23 -#define EXPONENT_MASK 0xFF -#define FRACTION_SHIFT 0 -#define FRACTION_MASK 0x7FFFFF - -#define EXPONENT_INF_NAN 0xFF - -// separate out the 3 components of a float -float_components_t float_to_components(uint32_t f) { - float_components_t c; - c.exponent = (f >> EXPONENT_SHIFT) & EXPONENT_MASK; - c.fraction = (f >> FRACTION_SHIFT) & FRACTION_MASK; - c.sign = (f >> SIGN_SHIFT) & SIGN_MASK; - return c; -} - - -// build a float from its 3 components -uint32_t float_from_components(float_components_t f) { - return (f.sign << SIGN_SHIFT) - | (f.exponent << EXPONENT_SHIFT) | - (f.fraction << FRACTION_SHIFT); -} - -int is_nan(float_components_t f) { - return f.exponent == EXPONENT_INF_NAN && f.fraction != 0; -} - -int is_positive_infinity(float_components_t f) { - return f.exponent == EXPONENT_INF_NAN && f.fraction == 0 && f.sign == 0; -} - -int is_negative_infinity(float_components_t f) { - return f.exponent == EXPONENT_INF_NAN && f.fraction == 0 && f.sign == 1; -} - -int is_zero(float_components_t f) { - return f.exponent == 0 && f.fraction == 0; -} diff --git a/lab03/float_less.mk b/lab03/float_less.mk deleted file mode 120000 index 586227a..0000000 --- a/lab03/float_less.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_less/files.ln/float_less.mk \ No newline at end of file diff --git a/lab03/float_print b/lab03/float_print deleted file mode 100755 index d23b42d..0000000 Binary files a/lab03/float_print and /dev/null differ diff --git a/lab03/float_print.c b/lab03/float_print.c deleted file mode 100644 index 4e97afd..0000000 --- a/lab03/float_print.c +++ /dev/null @@ -1,46 +0,0 @@ -// Print out a float. - -#include -#include -#include -#include - -#include "floats.h" - -// SAMPLE SOLUTION - -void float_print(uint32_t f); - -// -// We use this union to obtain the raw bits of a float -// by storing the float in the f field -// and then using the u field to obtain the bits -// - -union overlay { - float f; - uint32_t u; -}; - -int main(int argc, char *argv[]) { - for (int arg = 1; arg < argc; arg++) { - union overlay input; - - input.f = atof(argv[arg]); - float_print(input.u); - } - - return 0; -} - -// float_print is given the bits of a float as a uint32_t -// it prints out the float in the same format as "%.9g\n" -// using only putchar & puts -void float_print(uint32_t bits) { - // cheating to produce autotest expected output - union overlay input; - input.u = bits; - printf("%.9g\n", input.f); -} - - diff --git a/lab03/float_print.mk b/lab03/float_print.mk deleted file mode 120000 index 3d76e8f..0000000 --- a/lab03/float_print.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_print/files.ln/float_print.mk \ No newline at end of file diff --git a/lab03/floats.h b/lab03/floats.h deleted file mode 120000 index 66cd391..0000000 --- a/lab03/floats.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_bits/files.ln/floats.h \ No newline at end of file diff --git a/lab03/test_float_2048.c b/lab03/test_float_2048.c deleted file mode 120000 index 3e57834..0000000 --- a/lab03/test_float_2048.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_2048/files.ln/test_float_2048.c \ No newline at end of file diff --git a/lab03/test_float_bits.c b/lab03/test_float_bits.c deleted file mode 120000 index 642eb55..0000000 --- a/lab03/test_float_bits.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_bits/files.ln/test_float_bits.c \ No newline at end of file diff --git a/lab03/test_float_less.c b/lab03/test_float_less.c deleted file mode 120000 index 3af7bad..0000000 --- a/lab03/test_float_less.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_less/files.ln/test_float_less.c \ No newline at end of file diff --git a/lab03/test_float_print.c b/lab03/test_float_print.c deleted file mode 120000 index 30012ac..0000000 --- a/lab03/test_float_print.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_print/files.ln/test_float_print.c \ No newline at end of file diff --git a/lab04/42.instructions b/lab04/42.instructions deleted file mode 100644 index f2d15a5..0000000 --- a/lab04/42.instructions +++ /dev/null @@ -1,5 +0,0 @@ --3422289878 --3422420991 -12 -65011720 --1 diff --git a/lab04/42.s b/lab04/42.s deleted file mode 100644 index e474bee..0000000 --- a/lab04/42.s +++ /dev/null @@ -1,7 +0,0 @@ -# print 42 - -main: - li $a0, 42 # printf("%d", 42); - li $v0, 1 - syscall - jr $ra # return diff --git a/lab04/add.instructions b/lab04/add.instructions deleted file mode 100644 index b65b3dc..0000000 --- a/lab04/add.instructions +++ /dev/null @@ -1,11 +0,0 @@ --3422027759 --3421962215 -19419168 -663585 --3422420991 -12 --3422289910 --3422420981 -12 -65011720 --1 diff --git a/lab04/bad_pun.c b/lab04/bad_pun.c deleted file mode 100644 index 47e8008..0000000 --- a/lab04/bad_pun.c +++ /dev/null @@ -1,9 +0,0 @@ -// A simple C program that attempts to be punny - -#include - -int main(void) { - printf("I MIPS you!\n"); - - return 0; -} diff --git a/lab04/bad_pun.s b/lab04/bad_pun.s deleted file mode 100644 index 85c17cb..0000000 --- a/lab04/bad_pun.s +++ /dev/null @@ -1,51 +0,0 @@ -# A MIPS program that prints out a phrase -# Lab 4 Rifa Jamal z5311190 - -# creation of strings -.data -# create a label -promp: .asciiz "I MIPS you!" -# ascii string, z means 0 terminated (NULL byte at end) -# if we didnt have the 'z' we'd need to put the NULL byte at the end ourselves - - -# since writing code, start with .text -# everything under .text header is code -.text - -# good practice to have .global main but don't need it -.globl main - -# entry point to function -main: - -# we need to print a string -# need to use system call - -# we need to write a string and tell syscall what to do -# do this by putting a value into $v0, 4 -# value 4 inside v0 will tell syscall to print a string -# but we also need to tell it what string to print -# so we put a value into a0, a0 is the value to print -# we need to put address of string into a0 but before that -# we need to create a string (see above .text) - -# dont want to use li, li is just a number, use la (load address) -la $a0, promp -li $v0, 4 # 4 is print string -syscall - -# print a new line, new line is a character - -li $a0, '\n' -li $v0, 11 #11 is for print char -syscall - -# return 0; -# return value goes into v0 -# set v0 to 0 - -li $v0, 0 - -# jr (jump register) to ra (return address) -jr $ra \ No newline at end of file diff --git a/lab04/count.c b/lab04/count.c deleted file mode 100644 index a1b9cea..0000000 --- a/lab04/count.c +++ /dev/null @@ -1,30 +0,0 @@ -// read a number n and print the integers 1..n one per line - -#include - -int main(void) { - int number, i; - - printf("Enter number: "); - scanf("%d", &number); - - i = 1; - while (i <= number) { - printf("%d\n", i); - i = i + 1; - } - - // note this is equivalent to: - /* - - start: - if (i > number) goto end; - printf("%d\n", i) - i++: - goto start; - end:; - - */ - - return 0; -} diff --git a/lab04/count.s b/lab04/count.s deleted file mode 100644 index 5d12ba1..0000000 --- a/lab04/count.s +++ /dev/null @@ -1,49 +0,0 @@ -# read a number n and print the integers 1..n one per line -# written by Rifa Jamal COMP1521 z5311190 - -main: # int main(void) - - # load counter - li $t0, 1 # i value - li $t1, 0 # kind of like initialing a name for the number enetered - - - - la $a0, prompt # printf("Enter a number: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", number); - syscall - move $t1, $v0 - -main__start: - - # while loop starts here - # have a branch// see modified while loop in count.c - - bgt $t0, $t1, main__end # if i > number then target - - move $a0, $t0 # move value you're printing (i) to a0 - # li $a0, #printf("%d\n", i) - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - #use add or add immediate to increment i - addi $t0, $t0, 1 # $t0 = $t0 + 1 (i = i + 1) - - # go back to start (of loop) using jump - j main__start # jump to target - -main__end: - -end: - jr $ra # return - - .data -prompt: - .asciiz "Enter a number: " diff --git a/lab04/grade.c b/lab04/grade.c deleted file mode 100644 index 71d1d6a..0000000 --- a/lab04/grade.c +++ /dev/null @@ -1,24 +0,0 @@ -// read a mark and print the corresponding UNSW grade - -#include - -int main(void) { - int mark; - - printf("Enter a mark: "); - scanf("%d", &mark); - - if (mark < 50) { - printf("FL\n"); - } else if (mark < 65) { - printf("PS\n"); - } else if (mark < 75) { - printf("CR\n"); - } else if (mark < 85) { - printf("DN\n"); - } else { - printf("HD\n"); - } - - return 0; -} diff --git a/lab04/grade.s b/lab04/grade.s deleted file mode 100644 index 5daa315..0000000 --- a/lab04/grade.s +++ /dev/null @@ -1,93 +0,0 @@ -# read a mark and print the corresponding UNSW grade -# written by Rifa Jamal COMP1521 z5311190 - -main: - - # name the mark entered - li $t0, 0 #mark - - la $a0, prompt # printf("Enter a mark: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", mark); - syscall - # move scanned in value into $t0 (name) - move $t0, $v0 - - - # adding code from here - # have if statements that is true, print and then goto end - - #if (mark < 50) - #load number for $t1 (i.e. what you are checking - li $t1, 50 - # need a brach to check - blt $t0, $t1, printfl # if $t0 < $t1 then target - - #if we didn't branch the code will not to to printfl and keep going - - #if (mark < 65) - li $t2, 65 - blt $t0, $t2, printps # if $t0 < $t1 then target - - #if (mark < 75) - li $t3, 75 - blt $t0, $t3, printcr # if $t0 < $t1 then target - - #if (mark < 85) - li $t4, 85 - blt $t0, $t4, printdn # if $t0 < $t1 then target - - #if (mark < 85) - li $t4, 85 - blt $t0, $t4, printdn # if $t0 < $t1 then target - - #if (mark <= 100) - li $t5, 100 - ble $t0, $t5, printhd # if <= $t1 then target - - -printfl: - la $a0, fl # printf("FL\n"); - li $v0, 4 - syscall - jr $ra # return - -printps: - la $a0, ps # printf("PS\n"); - li $v0, 4 - syscall - jr $ra # return - -printcr: - la $a0, cr # printf("PS\n"); - li $v0, 4 - syscall - jr $ra # return - -printdn: - la $a0, dn # printf("PS\n"); - li $v0, 4 - syscall - jr $ra # return - -printhd: - la $a0, hd # printf("PS\n"); - li $v0, 4 - syscall - jr $ra # return - - .data -prompt: - .asciiz "Enter a mark: " -fl: - .asciiz "FL\n" -ps: - .asciiz "PS\n" -cr: - .asciiz "CR\n" -dn: - .asciiz "DN\n" -hd: - .asciiz "HD\n" diff --git a/lab04/print10.instructions b/lab04/print10.instructions deleted file mode 100644 index 36ed484..0000000 --- a/lab04/print10.instructions +++ /dev/null @@ -1,13 +0,0 @@ --3422027775 --3607035893 --4024434679 -532513 --3422420991 -12 --3422289910 --3422420981 -12 --3740794879 -67239927 -65011720 --1 diff --git a/lab04/print_for_challenge.instructions b/lab04/print_for_challenge.instructions deleted file mode 100644 index 8ec9009..0000000 --- a/lab04/print_for_challenge.instructions +++ /dev/null @@ -1,11 +0,0 @@ --3422289848 --3422420981 -12 --3422289815 --3422420981 -12 --3422289910 --3422420981 -12 -65011720 --1 diff --git a/lab04/print_for_challenge.s b/lab04/print_for_challenge.s deleted file mode 100644 index c42f79a..0000000 --- a/lab04/print_for_challenge.s +++ /dev/null @@ -1,17 +0,0 @@ -# print a string without using pre-initialized data -# for the dynamic load challenge exercise - -main: - li $a0, 'H' # printf("%c", 'Hi'); - li $v0, 11 - syscall - - li $a0, 'i' # printf("%c", 'i'); - li $v0, 11 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - jr $ra diff --git a/lab04/seven_eleven b/lab04/seven_eleven deleted file mode 100755 index 752aa30..0000000 Binary files a/lab04/seven_eleven and /dev/null differ diff --git a/lab04/seven_eleven.c b/lab04/seven_eleven.c deleted file mode 100644 index 7052fad..0000000 --- a/lab04/seven_eleven.c +++ /dev/null @@ -1,71 +0,0 @@ -// Read a number and print positive multiples of 7 or 11 < n - -#include - - -int main(void) { - int number, i; - - printf("Enter number: "); - scanf("%d", &number); - - i = 1; - while (i < number) { - if (i % 7 == 0 || i % 11 == 0) { - printf("%d\n", i); - } - i = i + 1; - } - - return 0; -} - - -/* -int main(void) { - - int number; - printf("Enter number: "); - scanf("%d", &number); - - //counter to count up from 1 to number - int i = 1; - - int counter_7 = 1; - int j_7 = 1; - - int counter_11 = 1; - int j_11 = 1; - - while (i < number) { - - //check 7 - if ((i - (7*j_7)) == 0) { - printf("%d\n", i); - } - counter_7++; - if(counter_7 == 7) { - counter_7 = 0; - j_7++; - } - - //check 11 - if ((i - (11*j_11)) == 0) { - printf("%d\n", i); - } - counter_11++; - if(counter_11 == 11) { - counter_11 = 0; - j_11++; - } - - i++; - - } - - return 0; -} -*/ - - - diff --git a/lab04/seven_eleven.s b/lab04/seven_eleven.s deleted file mode 100644 index dee60fe..0000000 --- a/lab04/seven_eleven.s +++ /dev/null @@ -1,50 +0,0 @@ -# Read a number and print positive multiples of 7 or 11 < n - -main: # int main(void) { - - # load counter - li $s0, 1 # i value - #li $s1, 0 # kind of like initialing a name for the number enetered - - la $a0, prompt # printf("Enter a number: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", number); - syscall - move $s1, $v0 #move value of scanned number into $t1 - -loop__start: # while loop starts here - - bge $s0, $s1, end # if i > number then go to main__end - - rem $s2, $s0, 7 # $t3 = $t1 % $t2 (i % 7) - beq $s2, 0, print__number # if t3 == 0 then target - - rem $s3, $s0, 11 # $t3 = $t1 % $t2 (i % 7) - beq $s3, 0, print__number # if t3 == 0 then target - - #increment i - addi $s0, $s0, 1 # $t0 = $t0 + 1 (i = i + 1) - # go back to start (of loop) using jump - b loop__start # jump to target - -print__number: - - move $a0, $s0 # printf("%d", i); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - addi $s0, $s0, 1 # $t0 = $t0 + 1 (i = i + 1) - b loop__start # jump to target - -end: - jr $ra # return - - .data -prompt: - .asciiz "Enter a number: " diff --git a/lab04/sum_100_squares.instructions b/lab04/sum_100_squares.instructions deleted file mode 100644 index f6ccfeb..0000000 --- a/lab04/sum_100_squares.instructions +++ /dev/null @@ -1,16 +0,0 @@ --3422027776 --3421962240 --3604938651 --4024434683 --2396432382 -17514528 --3738632191 -67239931 -532513 --3422420991 -12 --3422289910 --3422420981 -12 -65011720 --1 diff --git a/lab04/tetrahedral.c b/lab04/tetrahedral.c deleted file mode 100644 index a66b487..0000000 --- a/lab04/tetrahedral.c +++ /dev/null @@ -1,30 +0,0 @@ -// Read a number n and print the first n tetrahedral numbers -// https://en.wikipedia.org/wiki/Tetrahedral_number - -#include - -int main(void) { - int i, j, n, total, how_many; - - printf("Enter how many: "); - scanf("%d", &how_many); - - n = 1; - - while (n <= how_many) { - total = 0; - j = 1; - - while (j <= n) { - i = 1; - while (i <= j) { - total = total + i; - i = i + 1; - } - j = j + 1; - } - printf("%d\n", total); - n = n + 1; - } - return 0; -} diff --git a/lab04/tetrahedral.s b/lab04/tetrahedral.s deleted file mode 100644 index d497679..0000000 --- a/lab04/tetrahedral.s +++ /dev/null @@ -1,26 +0,0 @@ -# Read a number n and print the first n tetrahedral numbers -# https://en.wikipedia.org/wiki/Tetrahedral_number - -main: # int main(void) { - - la $a0, prompt # printf("Enter how many: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", number); - syscall - - li $a0, 42 # printf("%d", 42); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - -end: - jr $ra # return - - .data -prompt: - .asciiz "Enter how many: " diff --git a/lab05/bubblesort.c b/lab05/bubblesort.c deleted file mode 120000 index 3d89d99..0000000 --- a/lab05/bubblesort.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bubblesort/files.ln/bubblesort.c \ No newline at end of file diff --git a/lab05/bubblesort.s b/lab05/bubblesort.s deleted file mode 100644 index 0c7b46c..0000000 --- a/lab05/bubblesort.s +++ /dev/null @@ -1,55 +0,0 @@ -# read 10 numbers into an array -# bubblesort them -# then print the 10 numbers - -# i in register $t0 -# registers $t1, $t2 & $t3 used to hold temporary results - -main: - - li $t0, 0 # i = 0 -loop0: - bge $t0, 10, end0 # while (i < 10) { - - li $v0, 5 # scanf("%d", &numbers[i]); - syscall # - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - sw $v0, ($t3) # store entered number in array - - add $t0, $t0, 1 # i++; - b loop0 # } -end0: - - - # PUT YOUR CODE HERE - - - li $t0, 0 # i = 0 -loop1: - bge $t0, 10, end1 # while (i < 10) { - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - lw $a0, ($t3) # load numbers[i] into $a0 - li $v0, 1 # printf("%d", numbers[i]) - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - add $t0, $t0, 1 # i++ - b loop1 # } -end1: - - jr $31 # return - -.data - -numbers: - .word 0 0 0 0 0 0 0 0 0 0 # int numbers[10] = {0}; - diff --git a/lab05/numbers1.txt b/lab05/numbers1.txt deleted file mode 120000 index 24c9fa1..0000000 --- a/lab05/numbers1.txt +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/print_bigger/files.ln/numbers1.txt \ No newline at end of file diff --git a/lab05/numbers2.txt b/lab05/numbers2.txt deleted file mode 120000 index bd11d9a..0000000 --- a/lab05/numbers2.txt +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/print_bigger/files.ln/numbers2.txt \ No newline at end of file diff --git a/lab05/print_bigger.c b/lab05/print_bigger.c deleted file mode 120000 index 8340c8c..0000000 --- a/lab05/print_bigger.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/print_bigger/files.ln/print_bigger.c \ No newline at end of file diff --git a/lab05/print_bigger.s b/lab05/print_bigger.s deleted file mode 100644 index a18dea1..0000000 --- a/lab05/print_bigger.s +++ /dev/null @@ -1,65 +0,0 @@ -# COMP1521 Lab Rifa Jamal (z5311190) -# Written on 8/07/2020 -# Week 5 - -# Read 10 numbers into an array -# then print the numbers which are -# larger than the last number read. - -# i in register $t0 -# registers $t1, $t2 & $t3 used to hold temporary results - -main: - - li $t0, 0 # i = 0 -loop0: - bge $t0, 10, end0 # while (i < 10) { - - li $v0, 5 # scanf("%d", &numbers[i]); - syscall # - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - sw $v0, ($t3) # store entered number in array - - move $t4, $v0 # last_number = numbers[i] - - add $t0, $t0, 1 # i++; - b loop0 # } - -end0: - - - li $t0, 0 # i = 0 -loop1: - bge $t0, 10, end1 # while (i < 10) { - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - lw $a0, ($t3) # load numbers[i] into $a0 - - blt $a0, $t4, loop_increment # if (numbers[i] >= last_number) - - li $v0, 1 # printf("%d", numbers[i]) - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - -loop_increment: - - add $t0, $t0, 1 # i++ - b loop1 # } - -end1: - - jr $ra # return - -.data - -numbers: - .word 0 0 0 0 0 0 0 0 0 0 # int numbers[10] = {0}; - diff --git a/lab05/sorted.txt b/lab05/sorted.txt deleted file mode 120000 index ade3455..0000000 --- a/lab05/sorted.txt +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/unordered/files.ln/sorted.txt \ No newline at end of file diff --git a/lab05/swap_numbers.c b/lab05/swap_numbers.c deleted file mode 120000 index 6f7b851..0000000 --- a/lab05/swap_numbers.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/swap_numbers/files.ln/swap_numbers.c \ No newline at end of file diff --git a/lab05/swap_numbers.s b/lab05/swap_numbers.s deleted file mode 100644 index 71b2b23..0000000 --- a/lab05/swap_numbers.s +++ /dev/null @@ -1,80 +0,0 @@ -# COMP1521 Lab Rifa Jamal (z5311190) -# Written on 8/07/2020 -# Week 5 - -# read 10 numbers into an array -# swap any pairs of of number which are out of order -# then print the 10 numbers - -# i in register $t0, -# registers $t1 - $t3 used to hold temporary results - -main: - - li $t0, 0 # i = 0 -loop0: - bge $t0, 10, end0 # while (i < 10) { - - li $v0, 5 # scanf("%d", &numbers[i]); - syscall # - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - sw $v0, ($t3) # store entered number in array - - add $t0, $t0, 1 # i++; - b loop0 # goto start of loop0 -end0: - - li $t0, 1 # i = 1 - -loop2: - bge $t0, 10, end2 # while (i < 10) { - - mul $t1, $t0, 4 # finrd row size - la $t2, numbers # load address of array into Tt2 - add $t3, $t1, $t2 # $t3 = &numbers[i] - lw $t6, ($t3) # y = numbers[i] - - sub $t4, $t3, 4 # $t4 = &numbers[i - 1] - lw $t7, ($t4) # x = numbers[i - 1] - - bge $t6, $t7, loop2_increment # if (x < y) { - - sw $t7, ($t3) # numbers[i] = y - sw $t6, ($t4) # numbers[i - 1] = x - -loop2_increment: - add $t0, $t0, 1 # i++; - b loop2 # goto start of loop2 -end2: - - li $t0, 0 # i = 0 - -loop1: - bge $t0, 10, end1 # while (i < 10) { - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - lw $a0, ($t3) # load numbers[i] into $a0 - - li $v0, 1 # printf("%d", numbers[i]) - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - add $t0, $t0, 1 # i++ - b loop1 # } -end1: - - jr $31 # return - -.data - -numbers: - .word 0 0 0 0 0 0 0 0 0 0 # int numbers[10] = {0}; - diff --git a/lab05/unordered.c b/lab05/unordered.c deleted file mode 120000 index 4d27000..0000000 --- a/lab05/unordered.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/unordered/files.ln/unordered.c \ No newline at end of file diff --git a/lab05/unordered.s b/lab05/unordered.s deleted file mode 100644 index 01b0970..0000000 --- a/lab05/unordered.s +++ /dev/null @@ -1,67 +0,0 @@ -# COMP1521 Lab Rifa Jamal (z5311190) -# Written on 8/07/2020 -# Week 5 - -# Read 10 numbers into an array -# print 0 if they are in non-decreasing order -# print 1 otherwise - -# i in register $t0 - -main: - - li $t0, 0 # i = 0 -loop0: - bge $t0, 10, end0 # while (i < 10) { - - li $v0, 5 # scanf("%d", &numbers[i]); - syscall # - - mul $t1, $t0, 4 # calculate &numbers[i] - la $t2, numbers # - add $t3, $t1, $t2 # - sw $v0, ($t3) # store entered number in array - - add $t0, $t0, 1 # i++; - b loop0 # goto start of loop0 -end0: - - li $t5, 0 # swapped = 0 - li $t0, 1 # i = 1 - -loop1: - bge $t0, 10, end1 # while (i < 10) { - - mul $t1, $t0, 4 # - la $t2, numbers # - add $t3, $t1, $t2 # $t3 = &numbers[i] - lw $t6, ($t3) # y = numbers[i] - - sub $t4, $t3, 4 # $t4 = &numbers[i - 1] - lw $t7, ($t4) # x = numbers[i - 1] - - bge $t6, $t7, loop1_increment # if (x < y) { - li $t5, 1 # swapped = 1 - -loop1_increment: - - add $t0, $t0, 1 # i++; - b loop1 # goto start of loop1 -end1: - - move $a0, $t5 # load swapped into $a0 - li $v0, 1 # printf("%d", numbers[i]) - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - jr $ra # return - - -.data - -numbers: - .word 0 0 0 0 0 0 0 0 0 0 # int numbers[10] = {0}; - diff --git a/lab07/factorial.c b/lab07/factorial.c deleted file mode 120000 index 656760c..0000000 --- a/lab07/factorial.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/factorial/files.ln/factorial.c \ No newline at end of file diff --git a/lab07/factorial.s b/lab07/factorial.s deleted file mode 100644 index c41c332..0000000 --- a/lab07/factorial.s +++ /dev/null @@ -1,67 +0,0 @@ -# Recursive factorial function -# n < 1 yields n! = 1 -# $s0 is used for n -# we use an s register because the convention is their value -# is preserved across function calls -# f is in $t0 - -# DO NOT CHANGE THE CODE IN MAIN - -main: - addi $sp, $sp, -8 # create stack frame - sw $ra, 4($sp) # save return address - sw $s0, 0($sp) # save $s0 - - li $s0, 0 - la $a0, msg1 - li $v0, 4 - syscall # printf(Enter n: ") - - li $v0, 5 - syscall # scanf("%d", &n) - move $s0, $v0 - - move $a0, $s0 # factorial(n) - jal factorial # - move $t0, $v0 # - - move $a0, $s0 - li $v0, 1 - syscall # printf ("%d", n) - - la $a0, msg2 - li $v0, 4 - syscall # printf("! = ") - - move $a0, $t0 - li $v0, 1 - syscall # printf ("%d", f) - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - # clean up stack frame - lw $s0, 0($sp) # restore $s0 - lw $ra, 4($sp) # restore $ra - addi $sp, $sp, 8 # restore sp - - li $v0, 0 # return 0 - jr $ra - - .data -msg1: .asciiz "Enter n: " -msg2: .asciiz "! = " - - -# DO NOT CHANGE CODE ABOVE HERE - - - .text -factorial: - # ADD CODE TO CREATE STACK FRAME - - # ADD CODE FORFUNCTION HERE - - # ADD CODE TO REMOVE STACK FRAME - jr $ra diff --git a/lab07/lookup.c b/lab07/lookup.c deleted file mode 120000 index 654fa4e..0000000 --- a/lab07/lookup.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/lookup/files.ln/lookup.c \ No newline at end of file diff --git a/lab07/lookup.s b/lab07/lookup.s deleted file mode 100644 index db7f4e4..0000000 --- a/lab07/lookup.s +++ /dev/null @@ -1,106 +0,0 @@ -# Lab 7 COMP1521 -# Rifa Jamal z5311190 - -# Read 2 numbers and use them as indices into a 2d-array -# x in $t0, y in $t1 -main: - la $a0, prompt_x # printf("Enter x: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", &x); - syscall # - move $t0, $v0 - - la $a0, prompt_y # printf("Enter y: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", &y); - syscall # - move $t1, $v0 - - - # find address of array[x][y] - - # row position is $t0 - # col position is $t1 - - #find row size and offset - li $t9, 4 - mul $t3, $t9, 24 - mul $t0, $t0, $t3 - - #find col size and offset - mul $t1, $t1, 4 - - # add row and col offset - add $t5, $t0, $t1 - #load address of array - la $t6, array - # add offset to array address - add $t5, $t5, $t6 - - #load word stored in array[x][y] - lw $t7, ($t5) - - #print the value - move $a0, $t7 - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n') - li $v0, 11 - syscall - - li $v0, 0 # return 0 - jr $31 - -.data -prompt_x: - .asciiz "Enter x: " -prompt_y: - .asciiz "Enter y: " -array: - .word 9 4 3 2 5 1 1 4 3 1 2 6 7 5 6 2 8 1 8 3 4 1 1 1 - .word 7 3 9 6 6 2 4 8 6 8 1 9 8 2 9 5 9 8 9 9 2 3 1 1 - .word 1 4 6 5 4 2 9 5 7 9 5 6 4 1 6 9 6 1 9 3 8 3 1 2 - .word 3 3 3 9 7 3 7 1 3 2 3 7 7 3 2 5 8 1 4 2 4 5 9 4 - .word 5 7 6 9 4 2 4 9 4 7 9 2 8 1 6 2 7 4 9 7 1 9 3 5 - .word 8 3 8 9 3 4 6 2 4 1 3 3 2 1 2 4 2 7 8 6 8 6 9 9 - .word 9 5 8 9 7 5 6 6 2 9 5 1 1 8 6 8 3 4 1 1 5 9 5 2 - .word 3 2 4 1 4 8 2 8 7 6 7 8 8 3 8 2 6 5 5 5 5 9 5 3 - .word 7 1 3 9 8 8 6 3 1 7 6 5 6 9 3 8 1 5 7 6 7 7 5 6 - .word 4 6 5 7 4 1 4 7 3 5 5 7 9 6 8 4 3 1 9 9 2 6 8 9 - .word 2 3 8 5 8 8 7 1 8 1 1 8 2 2 3 9 7 6 7 9 3 2 6 5 - .word 1 4 7 4 7 7 7 7 9 9 8 9 5 5 3 3 9 5 8 7 7 6 1 7 - .word 5 3 8 7 5 6 1 9 5 6 3 3 5 9 9 5 4 1 3 8 1 1 1 4 - .word 9 8 1 7 5 1 7 4 9 7 4 8 2 5 9 3 6 3 6 3 2 7 3 2 - .word 1 6 1 4 2 9 6 1 3 2 5 7 3 9 4 4 6 5 9 8 4 5 1 4 - .word 7 7 7 2 1 6 1 3 9 4 4 6 6 6 3 9 3 8 2 8 8 4 8 7 - .word 7 8 7 9 3 5 7 1 1 4 1 4 9 6 7 3 8 5 1 7 9 2 2 2 - .word 2 4 6 5 7 3 4 6 1 7 2 5 1 7 1 2 9 6 7 8 5 4 5 7 - .word 2 4 4 9 2 8 1 9 5 9 5 9 8 3 4 7 6 7 5 2 9 9 5 5 - .word 8 4 2 6 3 8 8 3 6 3 2 4 5 1 8 6 6 4 5 8 4 6 8 5 - .word 7 7 9 8 4 1 1 3 8 8 7 6 3 8 1 2 2 4 4 5 3 5 9 9 - .word 5 7 1 7 5 5 8 1 4 6 5 7 5 9 3 7 4 8 6 4 1 6 7 1 - .word 4 5 3 3 1 2 5 3 1 5 7 6 6 2 8 8 8 3 6 3 1 2 6 3 - .word 9 5 3 4 7 2 9 9 8 6 2 5 9 3 1 8 6 9 6 3 3 2 3 3 - .word 8 6 5 3 3 7 6 3 3 9 1 4 7 5 1 6 5 1 6 8 8 1 9 7 - .word 4 7 5 9 1 7 6 9 5 2 3 7 3 8 8 3 9 8 5 6 1 6 6 9 - .word 2 8 6 9 3 3 6 9 4 5 2 6 3 8 3 9 6 7 6 5 6 8 2 6 - .word 4 8 6 4 5 3 9 4 3 4 7 9 9 4 5 8 6 6 3 4 7 1 3 4 - .word 7 4 6 7 1 9 6 2 8 4 5 6 7 6 4 1 6 3 1 2 5 9 2 1 - .word 2 8 9 1 6 5 1 7 2 3 3 5 4 8 6 1 9 8 5 8 1 4 4 7 - .word 8 8 2 9 9 4 8 8 9 2 6 4 2 8 1 2 3 3 9 5 3 1 1 1 - .word 3 9 5 7 7 9 7 3 4 2 1 8 6 3 6 9 3 3 4 2 5 1 2 3 - .word 4 4 6 4 5 8 1 7 4 4 6 6 9 7 9 4 3 6 6 4 9 8 2 6 - .word 3 8 2 2 7 4 3 8 7 4 1 6 6 2 3 5 2 1 8 4 6 4 8 6 - .word 5 2 5 6 5 9 3 3 8 1 3 8 2 9 2 8 9 7 2 7 5 5 7 7 - .word 2 7 6 4 3 2 1 4 6 3 7 5 7 7 5 6 4 6 8 2 9 3 6 1 - .word 6 4 4 6 1 4 2 6 3 7 9 9 4 4 2 1 8 1 4 4 2 7 4 9 - .word 3 8 5 2 3 9 2 4 8 9 3 3 6 2 3 3 1 8 5 8 8 5 1 9 - .word 1 5 8 1 4 9 2 4 9 5 7 6 7 4 8 9 1 3 8 6 4 4 9 9 - .word 5 6 7 8 3 2 9 1 1 7 7 6 9 7 7 7 8 8 3 3 8 9 9 1 - .word 8 2 5 9 1 1 7 6 3 6 7 7 7 2 4 5 5 2 1 1 1 7 4 3 - .word 8 9 4 5 4 6 2 5 3 7 5 1 6 7 2 8 5 6 2 2 1 7 6 2 diff --git a/lab07/sieve.c b/lab07/sieve.c deleted file mode 120000 index 6473392..0000000 --- a/lab07/sieve.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/sieve/files.ln/sieve.c \ No newline at end of file diff --git a/lab07/sieve.s b/lab07/sieve.s deleted file mode 100644 index 881aa95..0000000 --- a/lab07/sieve.s +++ /dev/null @@ -1,80 +0,0 @@ -# Lab 7 COMP1521 -# Rifa Jamal z5311190 - -# Sieve of Eratosthenes -# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes - -# $s0 = i -# $s1 = j - -main: - li $t0, 1000 - - #li $s0, 0 # int i = 0; - -loop1: - bge $s0, $t0, endloop1 - - la $t1, prime - add $t1, $t1, $s0 - li $t3, 1 - sb $t3, ($t1) #prime[i] = 1 - - addi $s0, $s0, 1 - b loop1 - -endloop1: - - li $s0, 2 # int i = 2 - -loop2_start: - - bge $s0, $t0, end - -if: - lb $t3, ($t1) - - la $t1, prime - add $t1, $t1, $s0 - lb $t3, ($t1) - - beq $t3, $0, loop2_increment # if prime[i] == 0 then target - - move $a0, $s0 # printf(i) - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n') - li $v0, 11 - syscall - - mul $s1, $s0, 2 #int j = 2*i - -loop3: - - bge $s1, 1000, loop2_increment # if >= $t1 then target - - # find prime[j] and set to 0 - - # row offset - la $t1, prime - add $t1, $t1, $s1 - li $t3, 0 - sb $t3, ($t1) #prime[j] = 0 - - add $s1, $s1, $s0 - b loop3 - -loop2_increment: - - addi $s0, $s0, 1 - b loop2_start - -end: - - li $v0, 0 # return 0 - jr $31 - -.data -prime: - .space 1000 \ No newline at end of file diff --git a/lab07/test.s b/lab07/test.s deleted file mode 100644 index f262402..0000000 --- a/lab07/test.s +++ /dev/null @@ -1,83 +0,0 @@ -#i in s0 -#j in s1 - -main: - - # PUT YOUR CODE - - li $t0, 1000 - -loop0: - - bge $s0, $t0, endloop0 - - la $t1, prime - #mul $t2, $s0, 4 - add $t1, $t1, $s0 - - li $t3, 1 - sb $t3, ($t1) - - addi $s0, $s0, 1 - - b loop0 - -endloop0: - - li $s0, 2 - -loop1: - - bge $s0, $t0, endloop1 - - la $t1, prime - #mul $t2, $s0, 4 - add $t1, $t1, $s0 - lb $t3, ($t1) - - beq $t3, $0, if0 - - move $a0, $s0 - li $v0, 1 - syscall - - li $a0, '\n' - li $v0, 11 - syscall - - mul $s1, $s0, 2 - -loop2: - - bge $s1, $t0, endloop2 - - la $t1, prime - #mul $t2, $s1, 4 - add $t1, $t1, $s1 - - li $t3, 0 - sb $t3, ($t1) - - add $s1, $s1, $s0 - - b loop2 - -endloop2: - -if0: - - addi $s0, $s0, 1 - - b loop1 - -endloop1: - - - li $v0, 0 # return 0 - jr $31 - -.data -prime: - .space 1000 - - diff --git a/lab08/1000.txt b/lab08/1000.txt deleted file mode 100644 index 1179824..0000000 --- a/lab08/1000.txt +++ /dev/null @@ -1,1000 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 -129 -130 -131 -132 -133 -134 -135 -136 -137 -138 -139 -140 -141 -142 -143 -144 -145 -146 -147 -148 -149 -150 -151 -152 -153 -154 -155 -156 -157 -158 -159 -160 -161 -162 -163 -164 -165 -166 -167 -168 -169 -170 -171 -172 -173 -174 -175 -176 -177 -178 -179 -180 -181 -182 -183 -184 -185 -186 -187 -188 -189 -190 -191 -192 -193 -194 -195 -196 -197 -198 -199 -200 -201 -202 -203 -204 -205 -206 -207 -208 -209 -210 -211 -212 -213 -214 -215 -216 -217 -218 -219 -220 -221 -222 -223 -224 -225 -226 -227 -228 -229 -230 -231 -232 -233 -234 -235 -236 -237 -238 -239 -240 -241 -242 -243 -244 -245 -246 -247 -248 -249 -250 -251 -252 -253 -254 -255 -256 -257 -258 -259 -260 -261 -262 -263 -264 -265 -266 -267 -268 -269 -270 -271 -272 -273 -274 -275 -276 -277 -278 -279 -280 -281 -282 -283 -284 -285 -286 -287 -288 -289 -290 -291 -292 -293 -294 -295 -296 -297 -298 -299 -300 -301 -302 -303 -304 -305 -306 -307 -308 -309 -310 -311 -312 -313 -314 -315 -316 -317 -318 -319 -320 -321 -322 -323 -324 -325 -326 -327 -328 -329 -330 -331 -332 -333 -334 -335 -336 -337 -338 -339 -340 -341 -342 -343 -344 -345 -346 -347 -348 -349 -350 -351 -352 -353 -354 -355 -356 -357 -358 -359 -360 -361 -362 -363 -364 -365 -366 -367 -368 -369 -370 -371 -372 -373 -374 -375 -376 -377 -378 -379 -380 -381 -382 -383 -384 -385 -386 -387 -388 -389 -390 -391 -392 -393 -394 -395 -396 -397 -398 -399 -400 -401 -402 -403 -404 -405 -406 -407 -408 -409 -410 -411 -412 -413 -414 -415 -416 -417 -418 -419 -420 -421 -422 -423 -424 -425 -426 -427 -428 -429 -430 -431 -432 -433 -434 -435 -436 -437 -438 -439 -440 -441 -442 -443 -444 -445 -446 -447 -448 -449 -450 -451 -452 -453 -454 -455 -456 -457 -458 -459 -460 -461 -462 -463 -464 -465 -466 -467 -468 -469 -470 -471 -472 -473 -474 -475 -476 -477 -478 -479 -480 -481 -482 -483 -484 -485 -486 -487 -488 -489 -490 -491 -492 -493 -494 -495 -496 -497 -498 -499 -500 -501 -502 -503 -504 -505 -506 -507 -508 -509 -510 -511 -512 -513 -514 -515 -516 -517 -518 -519 -520 -521 -522 -523 -524 -525 -526 -527 -528 -529 -530 -531 -532 -533 -534 -535 -536 -537 -538 -539 -540 -541 -542 -543 -544 -545 -546 -547 -548 -549 -550 -551 -552 -553 -554 -555 -556 -557 -558 -559 -560 -561 -562 -563 -564 -565 -566 -567 -568 -569 -570 -571 -572 -573 -574 -575 -576 -577 -578 -579 -580 -581 -582 -583 -584 -585 -586 -587 -588 -589 -590 -591 -592 -593 -594 -595 -596 -597 -598 -599 -600 -601 -602 -603 -604 -605 -606 -607 -608 -609 -610 -611 -612 -613 -614 -615 -616 -617 -618 -619 -620 -621 -622 -623 -624 -625 -626 -627 -628 -629 -630 -631 -632 -633 -634 -635 -636 -637 -638 -639 -640 -641 -642 -643 -644 -645 -646 -647 -648 -649 -650 -651 -652 -653 -654 -655 -656 -657 -658 -659 -660 -661 -662 -663 -664 -665 -666 -667 -668 -669 -670 -671 -672 -673 -674 -675 -676 -677 -678 -679 -680 -681 -682 -683 -684 -685 -686 -687 -688 -689 -690 -691 -692 -693 -694 -695 -696 -697 -698 -699 -700 -701 -702 -703 -704 -705 -706 -707 -708 -709 -710 -711 -712 -713 -714 -715 -716 -717 -718 -719 -720 -721 -722 -723 -724 -725 -726 -727 -728 -729 -730 -731 -732 -733 -734 -735 -736 -737 -738 -739 -740 -741 -742 -743 -744 -745 -746 -747 -748 -749 -750 -751 -752 -753 -754 -755 -756 -757 -758 -759 -760 -761 -762 -763 -764 -765 -766 -767 -768 -769 -770 -771 -772 -773 -774 -775 -776 -777 -778 -779 -780 -781 -782 -783 -784 -785 -786 -787 -788 -789 -790 -791 -792 -793 -794 -795 -796 -797 -798 -799 -800 -801 -802 -803 -804 -805 -806 -807 -808 -809 -810 -811 -812 -813 -814 -815 -816 -817 -818 -819 -820 -821 -822 -823 -824 -825 -826 -827 -828 -829 -830 -831 -832 -833 -834 -835 -836 -837 -838 -839 -840 -841 -842 -843 -844 -845 -846 -847 -848 -849 -850 -851 -852 -853 -854 -855 -856 -857 -858 -859 -860 -861 -862 -863 -864 -865 -866 -867 -868 -869 -870 -871 -872 -873 -874 -875 -876 -877 -878 -879 -880 -881 -882 -883 -884 -885 -886 -887 -888 -889 -890 -891 -892 -893 -894 -895 -896 -897 -898 -899 -900 -901 -902 -903 -904 -905 -906 -907 -908 -909 -910 -911 -912 -913 -914 -915 -916 -917 -918 -919 -920 -921 -922 -923 -924 -925 -926 -927 -928 -929 -930 -931 -932 -933 -934 -935 -936 -937 -938 -939 -940 -941 -942 -943 -944 -945 -946 -947 -948 -949 -950 -951 -952 -953 -954 -955 -956 -957 -958 -959 -960 -961 -962 -963 -964 -965 -966 -967 -968 -969 -970 -971 -972 -973 -974 -975 -976 -977 -978 -979 -980 -981 -982 -983 -984 -985 -986 -987 -988 -989 -990 -991 -992 -993 -994 -995 -996 -997 -998 -999 -1000 diff --git a/lab08/Makefile b/lab08/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/lab08/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/lab08/a.txt b/lab08/a.txt deleted file mode 100644 index 8a1218a..0000000 --- a/lab08/a.txt +++ /dev/null @@ -1,5 +0,0 @@ -1 -2 -3 -4 -5 diff --git a/lab08/count.binary b/lab08/count.binary deleted file mode 100644 index e69de29..0000000 diff --git a/lab08/create_binary_file b/lab08/create_binary_file deleted file mode 100755 index 8dc3751..0000000 Binary files a/lab08/create_binary_file and /dev/null differ diff --git a/lab08/create_binary_file.c b/lab08/create_binary_file.c deleted file mode 100644 index 4d53263..0000000 --- a/lab08/create_binary_file.c +++ /dev/null @@ -1,30 +0,0 @@ -// Lab 08 COMP1521 -// Written by Rifa Jamal z5311190 - -// C program, create_binary_file, takes at least one argument: -// a filename, -// and subsequently, integers in the range 0…255 inclusive specifying byte values. -// It should create a file of the specified name, containing the specified bytes. - -#include -#include -#include - -int main(int argc, char *argv[]) { - -// Make sure number of arguments is more than 1 -assert(argc > 1); - -FILE * outfile = fopen(argv[1], "w"); -if (outfile == NULL) { - perror(argv[1]); - exit(1); -} - -for (int i = 2; i < argc; i++) { - fputc(atoi(argv[i]), outfile); -} -fclose(outfile); -return 0; - -} diff --git a/lab08/create_binary_file.mk b/lab08/create_binary_file.mk deleted file mode 120000 index cb923b0..0000000 --- a/lab08/create_binary_file.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/create_binary_file/files.ln/create_binary_file.mk \ No newline at end of file diff --git a/lab08/create_integers_file b/lab08/create_integers_file deleted file mode 100755 index f67d33a..0000000 Binary files a/lab08/create_integers_file and /dev/null differ diff --git a/lab08/create_integers_file.c b/lab08/create_integers_file.c deleted file mode 100644 index ebb4aa0..0000000 --- a/lab08/create_integers_file.c +++ /dev/null @@ -1,39 +0,0 @@ -// Lab 08 COMP1521 -// Written by Rifa Jamal z5311190 - -// Write a C program, create_integers_file, which takes 3 arguments: -// 1. a filename, -// 2. the beginning of a range of integers, and -// 3. the end of a range of integers; -// and which creates a file of this name containing the specified integers - -// program also prints a suitable error message -// if given the wrong number of arguments, or if the file can not be created. - -#include -#include - -int main(int argc, char *argv[]) { - - FILE * outfile = fopen(argv[1], "w"); - - // change string character to int - int start = atoi(argv[2]); - int end = atoi(argv[3]); - - if (outfile == NULL) { - //perror(argv[1]); -> both are same - perror(argv[3]); - exit(1); - } - - for(int i = start; i <= end; i++) { - fprintf(outfile, "%d\n", i); - } - - fclose(outfile); - return 0; -} - - - diff --git a/lab08/create_integers_file.mk b/lab08/create_integers_file.mk deleted file mode 120000 index b0b9a86..0000000 --- a/lab08/create_integers_file.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/create_integers_file/files.ln/create_integers_file.mk \ No newline at end of file diff --git a/lab08/fortytwo.txt b/lab08/fortytwo.txt deleted file mode 100644 index 2568ecd..0000000 --- a/lab08/fortytwo.txt +++ /dev/null @@ -1,3 +0,0 @@ -40 -41 -42 diff --git a/lab08/hello.txt b/lab08/hello.txt deleted file mode 100644 index 10ddd6d..0000000 --- a/lab08/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello! diff --git a/lab08/hidden_strings b/lab08/hidden_strings deleted file mode 100755 index 9449dfc..0000000 Binary files a/lab08/hidden_strings and /dev/null differ diff --git a/lab08/hidden_strings.c b/lab08/hidden_strings.c deleted file mode 100644 index 59c452c..0000000 --- a/lab08/hidden_strings.c +++ /dev/null @@ -1,53 +0,0 @@ -// Lab 08 COMP1521 -// Written by Rifa Jamal z5311190 - -// C program, hidden_strings, which takes one argument, a filename; -// it should read that file, -// and print all sequences of length 4 or longer of consecutive byte values -// corresponding to printable ASCII characters. -// In other words, your program should read through the bytes of the file, -// and if it finds 4 bytes in a row containing printable characters, -// it should print those bytes, and any following bytes containing ASCII printable characters. - -// Print each sequence on a separate line. -// Assume ASCII printable characters are those for which the ctype.h function isprint(3) returns a non-zero value. -// Do not read the entire file into an array. - -#include -#include -#include - -int main(int argc, char *argv[]) { - - FILE * outfile = fopen(argv[1], "r"); - if (outfile == NULL) { - perror(argv[1]); - exit(1); - } - - int buffer[3]; - int printable_characters_read = 0; - int byte = 0; - - while ((byte = fgetc(outfile)) != EOF) { - if (isprint(byte)) { - if (printable_characters_read <= 2) { - buffer[printable_characters_read] = byte; - printable_characters_read++; - } else { - printf("%c%c%c%c", buffer[0], buffer[1], buffer[2], byte); - while (((byte = fgetc(outfile)) != EOF) && isprint(byte)) { - printf("%c", byte); - } - printable_characters_read = 0; - printf("\n"); - } - } else { - printable_characters_read = 0; - } - } - - fclose(outfile); - return 0; - -} \ No newline at end of file diff --git a/lab08/hidden_strings.mk b/lab08/hidden_strings.mk deleted file mode 120000 index 296af16..0000000 --- a/lab08/hidden_strings.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/hidden_strings/files.ln/hidden_strings.mk \ No newline at end of file diff --git a/lab08/last_line.c b/lab08/last_line.c deleted file mode 100644 index 19903c2..0000000 --- a/lab08/last_line.c +++ /dev/null @@ -1,53 +0,0 @@ -// SAMPLE SOLUTIONNNNNN - -// Print the last line of a file without reading the entire file - -#include -#include - -int main(int argc, char *argv[]) { - if (argc < 2) { - fprintf(stderr, "Usage: %s: ...\n", argv[0]); - exit(1); - } - - FILE *stream = fopen(argv[1], "r"); - if (stream == NULL) { - fprintf(stderr, "%s: ", argv[0]); - perror(argv[1]); - exit(1); - } - - // look backwards through the file for a '\n' - // or start of the file - // starting at the 2nd last byte - - long offset = -2; - while (fseek(stream, offset, SEEK_END) == 0 && fgetc(stream) != '\n') { - offset--; - } - - // write out all bytes after the '\n' or start of file - fseek(stream, offset + 1, SEEK_END); - int byte; - while ((byte = fgetc(stream)) != EOF) { - fputc(byte, stdout); - } - - fclose(stream); - - return 0; -} - - -//Write a C program, last_line, which takes one argument, a filename, and which should print the last line of that file. -// You program should not assume the last byte of the file is a newline character. -// Your program should handle extremely large files. It should not read the entire file. -//As this is a challenge exercise, marks will not be awarded for programs which read the entire file. - - -// Note -//If the last byte of file is not '\n', you should print all bytes after the last '\n' byte. -// If there is no '\n' byte, you should print the entire file. -// If the last byte of the file is '\n', you should print every byte after the previous (second last '\n') '\n' byte. -// If there is no previous '\n' byte, you should print the entire file. \ No newline at end of file diff --git a/lab08/last_line.mk b/lab08/last_line.mk deleted file mode 120000 index 267a3ff..0000000 --- a/lab08/last_line.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/last_line/files.ln/last_line.mk \ No newline at end of file diff --git a/lab08/print_bytes b/lab08/print_bytes deleted file mode 100755 index 48ab03a..0000000 Binary files a/lab08/print_bytes and /dev/null differ diff --git a/lab08/print_bytes.c b/lab08/print_bytes.c deleted file mode 100644 index 1fc67bf..0000000 --- a/lab08/print_bytes.c +++ /dev/null @@ -1,41 +0,0 @@ -// Lab 08 COMP1521 -// Written by Rifa Jamal z5311190 - -// C program, print_bytes, which takes one argument, a filename, -// and which should should read the specifed file -// and print one line for each byte of the file. -// The line should show the byte in decimal and hexadecimal. -// If that byte is a an ASCII printable character, its ASCII value should also be printed. - -// Assume ASCII printable characters are those for which -// the ctype.h function isprint(3) returns a non-zero value. - -#include -#include -#include - -int main(int argc, char *argv[]) { - - FILE * outfile = fopen(argv[1], "r"); - - if (outfile == NULL) { - perror(argv[1]); - exit(1); - } - - long bytes_read = 0; - int byte; - - while ((byte = fgetc(outfile)) != EOF) { - printf("byte %4ld: %3d 0x%02x", bytes_read, byte, byte); - - if (isprint(byte)) { - printf(" '%c'", byte); - } - printf("\n"); - bytes_read++; - } - - fclose(outfile); - return 0; -} \ No newline at end of file diff --git a/lab08/print_bytes.mk b/lab08/print_bytes.mk deleted file mode 120000 index 0a4d04a..0000000 --- a/lab08/print_bytes.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/print_bytes/files.ln/print_bytes.mk \ No newline at end of file diff --git a/lab08/secret.c b/lab08/secret.c deleted file mode 120000 index 3e2f0ff..0000000 --- a/lab08/secret.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/hidden_strings/files.ln/secret.c \ No newline at end of file diff --git a/lab08/test_file b/lab08/test_file deleted file mode 100644 index 067ba43..0000000 --- a/lab08/test_file +++ /dev/null @@ -1 +0,0 @@ -Hello˙˙Andrew \ No newline at end of file diff --git a/lab09/Makefile b/lab09/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/lab09/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/lab09/addi.c b/lab09/addi.c deleted file mode 100644 index a7bc744..0000000 --- a/lab09/addi.c +++ /dev/null @@ -1,56 +0,0 @@ -// Written by Rifa Jamal COMP1521 20T2 - -// Sample solution for COMP1521 lab exercises -// -// generate the opcode for an addi instruction - -#include -#include -#include -#include - -#include "addi.h" - -// return the MIPS opcode for addi $t,$s, i -uint32_t addi(int t, int s, int i) { - - // addi has an opcode of 001000 - // 001000ssssstttttIIIIIIIIIIIIIIII - - uint32_t result = 0x00000000; - uint32_t opcode = 0x20000000; - - if (i >= 0) { - result |= i; - t <<= 16; - result |= t; - s <<= 21; - result |= s; - result |= opcode; - } else { - i &= 0x0000ffff; - result |= i; - t <<= 16; - result |= t; - s <<= 21; - result |= s; - result |= opcode; - } - - return result; - -} - - -// SAMPLE SOLUTION: -/* - -// return the MIPS opcode for addi $t,$s, i -uint32_t addi(int t, int s, int i) { - return 0x20000000 | - ((uint32_t)s) << 21 | - ((uint32_t)t) << 16 | - (i & 0xFFFF); -} - -*/ \ No newline at end of file diff --git a/lab09/addi.h b/lab09/addi.h deleted file mode 120000 index 482012e..0000000 --- a/lab09/addi.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/addi/files.ln/addi.h \ No newline at end of file diff --git a/lab09/addi.mk b/lab09/addi.mk deleted file mode 120000 index 6411fed..0000000 --- a/lab09/addi.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/addi/files.ln/addi.mk \ No newline at end of file diff --git a/lab09/compile_if_needed.c b/lab09/compile_if_needed.c deleted file mode 100644 index 6ab7faf..0000000 --- a/lab09/compile_if_needed.c +++ /dev/null @@ -1,109 +0,0 @@ -// compile .c files specified as command line arguments if needed -// -// if my_program.c is speicified as an argument -// /usr/local/bin/dcc my_program.c -o my_program -// will be executed unless my_program exists -// and my_program's modification time is more recent than my_program.c - -#include -#include -#include -#include -#include -#include -#include -#include - -void compile_if_needed(char *c_file); -int is_compile_needed(char *c_file, char *binary); -void compile(char *c_file, char *binary); -char *get_binary_name(char *c_file); - -int main(int argc, char *argv[]) { - for (int arg = 1; arg < argc; arg++) { - compile_if_needed(argv[arg]); - } - return 0; -} - - -// compile a C file if needed -void compile_if_needed(char *c_file) { - char *binary = get_binary_name(c_file); - if (is_compile_needed(c_file, binary)) { - compile(c_file, binary); - } else { - printf("%s does not need compiling\n", c_file); - } - free(binary); -} - -// DO NOT CHANGE CODE ABOVE HERE - - - -// test if we need to recompile a C file -// return 1 if binary does not exist -// or modification time of C file more recent than binary -// return 0, otherwise -int is_compile_needed(char *c_file, char *binary) { - - return 1; // REPLACE ME WITH YOUR CODE - -} - - -// DO NOT CHANGE CODE BELOW HERE - -#define C_COMPILER "/usr/local/bin/dcc" - -// compile a C file -void compile(char *c_file, char *binary) { - pid_t pid; - extern char **environ; - char *cc_argv[] = {C_COMPILER, c_file, "-o", binary, NULL}; - - // print compile command - for (char **p = cc_argv; *p; p++) { - printf("%s ", *p); - } - printf("\n"); - - // run compile command - if (posix_spawn(&pid, C_COMPILER, NULL, NULL, cc_argv, environ) != 0) { - perror("spawn"); - exit(1); - } - - int exit_status; - if (waitpid(pid, &exit_status, 0) == -1) { - perror("waitpid"); - exit(1); - } - - if (exit_status != 0) { - fprintf(stderr, "compile failed\n"); - exit(1); - } -} - - -// give a string ending in .c -// return malloc-ed copy of string without .c - -char *get_binary_name(char *c_file) { - char *binary = strdup(c_file); - if (binary == NULL) { - perror(""); - exit(1); - } - - // remove .c suffix - char *last_dot = strrchr(binary, '.'); - if (last_dot == NULL || last_dot[1] != 'c' || last_dot[2] != '\0') { - fprintf(stderr, "'%s' does not end in .c\n", c_file); - exit(1); - } - *last_dot = '\0'; - return binary; -} diff --git a/lab09/compile_if_needed.mk b/lab09/compile_if_needed.mk deleted file mode 120000 index 527d3a3..0000000 --- a/lab09/compile_if_needed.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/compile_if_needed/files.ln/compile_if_needed.mk \ No newline at end of file diff --git a/lab09/file_modes.c b/lab09/file_modes.c deleted file mode 100644 index 8a38db2..0000000 --- a/lab09/file_modes.c +++ /dev/null @@ -1,95 +0,0 @@ -// Lab 09 Written by Rifa Jamal COMP1521 -// 20T2 - -// file_modes.c, which is given one or more pathnames -// as command line arguments. It should print -// one line for each pathnames which -// gives the permissions of the file or directory. - -#include -#include -#include -#include -#include - -void stat_file(char *pathname); - -int main(int argc, char *argv[]) { - - for (int arg = 1; arg < argc; arg++) { - stat_file(argv[arg]); - } - return 0; -} - -void stat_file(char *pathname) { - struct stat s; - if (stat(pathname, &s) != 0) { - perror(pathname); - exit(1); - } - printf( (S_ISDIR(s.st_mode)) ? "d" : "-"); - printf( (s.st_mode & S_IRUSR) ? "r" : "-"); - printf( (s.st_mode & S_IWUSR) ? "w" : "-"); - printf( (s.st_mode & S_IXUSR) ? "x" : "-"); - printf( (s.st_mode & S_IRGRP) ? "r" : "-"); - printf( (s.st_mode & S_IWGRP) ? "w" : "-"); - printf( (s.st_mode & S_IXGRP) ? "x" : "-"); - printf( (s.st_mode & S_IROTH) ? "r" : "-"); - printf( (s.st_mode & S_IWOTH) ? "w" : "-"); - printf( (s.st_mode & S_IXOTH) ? "x" : "-"); - printf(" %s", pathname); - printf("\n"); - -} - - -// SAMPLE SOLUTIONS: - -/* - -#include -#include -#include -#include -#include -#include - -void file_mode(char *pathname); - -int main(int argc, char *argv[]) { - for (int arg = 1; arg < argc; arg++) { - file_mode(argv[arg]); - } - return 0; -} - -// convert octal mode to -rwxrwxrwx string and print itm - -void file_mode(char *pathname) { - struct stat s; - if (stat(pathname, &s) != 0) { - perror(pathname); - exit(1); - } - mode_t mode = s.st_mode; - - char permissions[] = "?rwxrwxrwx"; - int n_permissions = strlen(permissions); - - if (S_ISREG(mode)) { - permissions[0] = '-'; - } else if (S_ISDIR(mode)) { - permissions[0] = 'd'; - } - - for (int i = 1; i < n_permissions; i++) { - if (!(mode & (1 << (i - 1)))) { - permissions[n_permissions - i] = '-'; - } - } - - printf("%s %s\n", permissions, pathname); -} - -*/ \ No newline at end of file diff --git a/lab09/file_modes.mk b/lab09/file_modes.mk deleted file mode 120000 index 5cfc8a1..0000000 --- a/lab09/file_modes.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/file_modes/files.ln/file_modes.mk \ No newline at end of file diff --git a/lab09/file_sizes.c b/lab09/file_sizes.c deleted file mode 100644 index 6a840d2..0000000 --- a/lab09/file_sizes.c +++ /dev/null @@ -1,36 +0,0 @@ -// Lab 09 Written by Rifa Jamal COMP1521 -// file_sizes.c, which is given one or more -// filenames as command line arguments. -// It should print one line for each filename which -// gives the size in bytes of the file. It should also -// print a line giving the combined number of bytes in the files. - -#include -#include -#include -#include -#include - -int stat_file(char *pathname); - -int main(int argc, char *argv[]) { - - long size = 0; - - for (int arg = 1; arg < argc; arg++) { - struct stat s; - if (stat(argv[arg], &s) != 0) { - perror(argv[arg]); - exit(1); - } - - printf("%s: %ld bytes\n",argv[arg], (long)s.st_size); - size+= (long)s.st_size; - } - - printf("Total: %ld bytes\n", size); - - return 0; - -} - diff --git a/lab09/file_sizes.mk b/lab09/file_sizes.mk deleted file mode 120000 index af35bdc..0000000 --- a/lab09/file_sizes.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/file_sizes/files.ln/file_sizes.mk \ No newline at end of file diff --git a/lab09/lsld.mk b/lab09/lsld.mk deleted file mode 120000 index b166c7a..0000000 --- a/lab09/lsld.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/lsld/files.ln/lsld.mk \ No newline at end of file diff --git a/lab09/test_addi.c b/lab09/test_addi.c deleted file mode 120000 index 2fbae5b..0000000 --- a/lab09/test_addi.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/addi/files.ln/test_addi.c \ No newline at end of file diff --git a/lab10/Makefile b/lab10/Makefile deleted file mode 100644 index 175076f..0000000 --- a/lab10/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# COMP1521 20T2 ... a general makefile for multiple exercises - -CC = dcc -EXERCISES ?= -CLEAN_FILES ?= - -.DEFAULT_GOAL = all -.PHONY: all - --include *.mk - -all: ${EXERCISES} - -clean: - -rm -f ${CLEAN_FILES} diff --git a/lab10/diary b/lab10/diary deleted file mode 100755 index 332bef6..0000000 Binary files a/lab10/diary and /dev/null differ diff --git a/lab10/diary.c b/lab10/diary.c deleted file mode 100644 index 7630da8..0000000 --- a/lab10/diary.c +++ /dev/null @@ -1,34 +0,0 @@ -// Lab 10 COMP1521 -// Written by Rifa Jamal (z5311190) -// on 8/08/2020 - -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char *argv[]) { - - char *home = getenv("HOME"); - - int size = snprintf(NULL, 0, "%s/.diary", home); - char *path = calloc(size + 1, sizeof(char)); - snprintf(path, size + 1, "%s/.diary", home); - - FILE *infile = fopen(path, "a"); - if (!infile) { - perror(path); - exit(1); - } - - int i = 1; - while (i < argc){ - fprintf(infile, "%s ", argv[i]); - i++; - } - fprintf(infile, "%s", "\n"); - return 0; -} \ No newline at end of file diff --git a/lab10/diary.mk b/lab10/diary.mk deleted file mode 100644 index 6ef7333..0000000 --- a/lab10/diary.mk +++ /dev/null @@ -1,3 +0,0 @@ -EXERCISES += diary -CLEAN_FILES += diary diary.o -diary: diary.c diff --git a/lab10/lru b/lab10/lru deleted file mode 100755 index 43df4f1..0000000 Binary files a/lab10/lru and /dev/null differ diff --git a/lab10/lru.c b/lab10/lru.c deleted file mode 100644 index 1940b1b..0000000 --- a/lab10/lru.c +++ /dev/null @@ -1,119 +0,0 @@ -// Lab 10 COMP1521 -// Written by Rifa Jamal (z5311190) -// on 8/08/2020 - -// Simulate LRU replacement of page frames - -#include -#include -#include -#include - -#define TRUE 1 -#define FALSE 0 - -// represent an entry in a simple inverted page table - -typedef struct ipt_entry { - int virtual_page; // == -1 if physical page free - int last_access_time; -} ipt_entry_t; - - -void lru(int n_physical_pages, int n_virtual_pages); -void access_page(int virtual_page, int access_time, int n_physical_pages, struct ipt_entry *ipt); - -int main(int argc, char *argv[]) { - if (argc != 3) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return 1; - } - lru(atoi(argv[1]), atoi(argv[2])); - return 0; -} - - -void lru(int n_physical_pages, int n_virtual_pages) { - printf("Simulating %d pages of physical memory, %d pages of virtual memory\n", - n_physical_pages, n_virtual_pages); - struct ipt_entry *ipt = malloc(n_physical_pages * sizeof *ipt); - assert(ipt); - - for (int i = 0; i < n_physical_pages; i++) { - ipt[i].virtual_page = -1; - ipt[i].last_access_time = -1; - } - - int virtual_page; - for (int access_time = 0; scanf("%d", &virtual_page) == 1; access_time++) { - assert(virtual_page >= 0 && virtual_page < n_virtual_pages); - access_page(virtual_page, access_time, n_physical_pages, ipt); - } -} - - -// if virtual_page is not in ipt, the first free page is used -// if there is no free page, the least-recently-used page is evicted -// -// a single line of output describing the page access is always printed -// the last_access_time in ipt is always updated - -void access_page(int virtual_page, int access_time, int n_physical_pages, struct ipt_entry *ipt) { - - // PUT YOUR CODE HERE TO HANDLE THE 3 cases - // - int page_in_use = FALSE; - for (int i = 0; i < n_physical_pages; i++) { - - // 1) The virtual page is already in a physical page - // There is already a physical page that holds virtual page - - if (ipt[i].virtual_page == virtual_page) { - ipt[i].last_access_time = access_time; - printf("Time %d: virtual page %d -> physical page %d\n", access_time, virtual_page, i); - page_in_use = TRUE; - break; - - // There is free space - } else if (ipt[i].virtual_page == -1) { - - // 2) The virtual page is not in a physical page, - // and there is free physical page - - ipt[i].virtual_page = virtual_page; - ipt[i].last_access_time = access_time; - printf("Time %d: virtual page %d loaded to physical page %d\n", access_time, virtual_page, i); - page_in_use = TRUE; - break; - } - } - // 3) The virtual page is not in a physical page, - // and there is no free physical page - if (page_in_use == FALSE) { - - // find least recently used page - int time = ipt[0].last_access_time; - int physical_page = 0; - // start from j = 1 and ipt[0] contains last_access_time - for (int j = 1; j < n_physical_pages; j++) { - if (ipt[j].last_access_time < time) { - time = ipt[j].last_access_time; - physical_page = j; - } - } - - int removed_physical_page = physical_page; - int prev_virtual_page = ipt[removed_physical_page].virtual_page; - - ipt[removed_physical_page].virtual_page = virtual_page; - ipt[removed_physical_page].last_access_time = access_time; - - printf("Time %d: virtual page %d - virtual page %d evicted - loaded to physical page %d\n", access_time, virtual_page, prev_virtual_page, removed_physical_page); - - page_in_use = TRUE; - } - - // don't forgot to update the last_access_time of the virtual_page - -} - diff --git a/lab10/lru.mk b/lab10/lru.mk deleted file mode 100644 index 78f6e26..0000000 --- a/lab10/lru.mk +++ /dev/null @@ -1,3 +0,0 @@ -EXERCISES += lru -CLEAN_FILES += lru lru.o -lru: lru.c diff --git a/lab10/page_table.mk b/lab10/page_table.mk deleted file mode 100644 index a5b3845..0000000 --- a/lab10/page_table.mk +++ /dev/null @@ -1,3 +0,0 @@ -EXERCISES += non_ascii -CLEAN_FILES += non_ascii non_ascii.o -non_ascii: non_ascii.c diff --git a/weekly_tests/test.s b/weekly_tests/test.s deleted file mode 100644 index a9dec36..0000000 --- a/weekly_tests/test.s +++ /dev/null @@ -1,148 +0,0 @@ -#mips minimum - -.text -.globl main - -main: - - li $t0, 0 # int x - li $t1, 0 # int y - - li $v0, 5 # scanf("%d", x); - syscall - move $t0, $v0 - - li $v0, 5 # scanf("%d", y); - syscall - move $t1, $v0 - - bge $t1, $t0, print_y # if y >= x then target - -print_x: - - move $a0, $t0 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j main_end - -print_y: - - move $a0, $t0 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - -main_end: - - jr $ra # return - - -############################################################### - -#mipscounting (butnot13) - -.text -.globl main - -main: - - li $t0, 0 # int x - li $t1, 0 # int y - - li $v0, 5 # scanf("%d", x); - syscall - move $t0, $v0 - - li $v0, 5 # scanf("%d", y); - syscall - move $t1, $v0 - - addi $t2, $t0, 1 # $t2 = $t0 + 0 - - bge $t1, $t2, main_end # if $t1 >= 2t1main_endtarget - -loop_start: - - beq $t2, 13, loop_increment # if $t2 == 13 then target - - move $a0, $t2 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - -loop_increment: - - addi $t2, $t2, 1 # i++; - j loop_start - -main_end: - - jr $ra # return - - -####################################################################### - - -#mipssquares - -.text -.globl main - -main: - - li $t0, 1 # int x - - li $v0, 5 # scanf("%d", x); - syscall - move $t0, $v0 - - li $t1, 0 # int i = 0 - -loop1: - - bge $t0, $t1, main_end # if x >= i then target - li $t2, 0 # int j = 0 - -loop2: - bge $t0, $t2, loop1_increment # if x >= j then target - - li $a0, '*' # printf("%c", '*'); - li $v0, 11 - syscall - -loop2_increment: - - addi $t2, $t2, 1 # j = j + 1 - j loop2 - -loop1_increment: - - addi $t1, $t1, 1 # i = 1 + 1; - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j loop1 - -main_end: - - jr $ra # return - - - - - - \ No newline at end of file diff --git a/weekly_tests/test03/Makefile b/weekly_tests/test03/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/weekly_tests/test03/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/weekly_tests/test03/bit_count b/weekly_tests/test03/bit_count deleted file mode 100755 index fbaff0b..0000000 Binary files a/weekly_tests/test03/bit_count and /dev/null differ diff --git a/weekly_tests/test03/bit_count.c b/weekly_tests/test03/bit_count.c deleted file mode 100644 index 1f5658a..0000000 --- a/weekly_tests/test03/bit_count.c +++ /dev/null @@ -1,36 +0,0 @@ -// count bits in a uint64_t - -#include -#include -#include - -// return how many 1 bits value contains -int bit_count(uint64_t value) { - // PUT YOUR CODE HERE - - uint64_t mask = 1; // 0b 0000 .... 0001 - int count = 0; - - // how this works: - // e.g. value = 0b 0101 0101 - // first iteration of for loop: - // 0101 0101 & 0000 0001 = 0000 0001 - // count = 1 - // value >>= 1 --> 0010 1010 - - //second iteration of for loop: - //0010 1010 & 0000 0001 = 0000 0000 - // hence count is still 1 - // value = 0001 0101 - - //and so on... - - while (value != 0) { - if ((value & mask) == 1) { - count++; - } - value >>= 1; - } - - return count; -} diff --git a/weekly_tests/test03/bit_count.mk b/weekly_tests/test03/bit_count.mk deleted file mode 120000 index 101d3a2..0000000 --- a/weekly_tests/test03/bit_count.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_count/files.ln/bit_count.mk \ No newline at end of file diff --git a/weekly_tests/test03/bit_swap b/weekly_tests/test03/bit_swap deleted file mode 100755 index 5d79389..0000000 Binary files a/weekly_tests/test03/bit_swap and /dev/null differ diff --git a/weekly_tests/test03/bit_swap.c b/weekly_tests/test03/bit_swap.c deleted file mode 100644 index 57cff63..0000000 --- a/weekly_tests/test03/bit_swap.c +++ /dev/null @@ -1,40 +0,0 @@ -// swap pairs of bits of a 64-bit value, using bitwise operators - -#include -#include -#include - -// return value with pairs of bits swapped -uint64_t bit_swap(uint64_t value) { - // PUT YOUR CODE HERE - - uint64_t mask = 1; // 0b 0000 .... 0001 - uint64_t bottom_bit = 0; - uint64_t top_bit = 0; - uint64_t swapped = 0; - - // example with 4 bits: 1010 - // bottom bit = 1010 & 0001 = 000 ->0 - // top bit = 1010 & 0010 = 00 ->1 0 - // swapped = 0000 | 0000 = 0000 - // swapped = 0000 | 00001 = 0001 - // i = 2 - - // bottom bit = 1010 & (0100) = 0 ->0 00 - // top bit = 1010 & (1000) = ->1 000 - // swapped = 0001 | (0000) = 0001 - // swapped = 0001 | (0100) = 0101 - - //hence 1010 became 0101 and the bits (in groups of two) have swapped - - for (int i = 0; i < 64; i+=2) { - bottom_bit = value & (mask << i); - top_bit = value & (mask << (i + 1)); - //shift positions of bottom and top bit and put into swapped - swapped = swapped | (bottom_bit << 1); - swapped = swapped | (top_bit >> 1); - } - - return swapped; - -} diff --git a/weekly_tests/test03/bit_swap.mk b/weekly_tests/test03/bit_swap.mk deleted file mode 120000 index 49e09cd..0000000 --- a/weekly_tests/test03/bit_swap.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_swap/files.ln/bit_swap.mk \ No newline at end of file diff --git a/weekly_tests/test03/short_swap b/weekly_tests/test03/short_swap deleted file mode 100755 index cfb72a3..0000000 Binary files a/weekly_tests/test03/short_swap and /dev/null differ diff --git a/weekly_tests/test03/short_swap.c b/weekly_tests/test03/short_swap.c deleted file mode 100644 index c75ece6..0000000 --- a/weekly_tests/test03/short_swap.c +++ /dev/null @@ -1,24 +0,0 @@ -// Swap bytes of a short - -#include -#include -#include - -// given uint16_t value return the value with its bytes swapped -uint16_t short_swap(uint16_t value) { - // PUT YOUR CODE HERE - - uint16_t mask = 0xff; //0b 0000 0000 1111 1111 - uint16_t swapped = 0; //0b 0000 0000 0000 0000 - - //e.g. let value be 0000 0000 1111 1111 - swapped = mask & value; // 0b 0000 0000 1111 1111 - swapped <<= 8; // 0b 1111 1111 0000 0000 - value >>= 8; // 0b 0000 0000 0000 0000 - swapped = swapped | (mask & value); - //mask & value = 0000 0000 0000 0000 - // hence, swapped = 1111 1111 0000 0000 - - return swapped; - -} diff --git a/weekly_tests/test03/short_swap.mk b/weekly_tests/test03/short_swap.mk deleted file mode 120000 index 7ed24ed..0000000 --- a/weekly_tests/test03/short_swap.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/short_swap/files.ln/short_swap.mk \ No newline at end of file diff --git a/weekly_tests/test03/test_bit_count.c b/weekly_tests/test03/test_bit_count.c deleted file mode 120000 index 20506d3..0000000 --- a/weekly_tests/test03/test_bit_count.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_count/files.ln/test_bit_count.c \ No newline at end of file diff --git a/weekly_tests/test03/test_bit_swap.c b/weekly_tests/test03/test_bit_swap.c deleted file mode 120000 index 553ae37..0000000 --- a/weekly_tests/test03/test_bit_swap.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_swap/files.ln/test_bit_swap.c \ No newline at end of file diff --git a/weekly_tests/test03/test_short_swap.c b/weekly_tests/test03/test_short_swap.c deleted file mode 120000 index f6854ca..0000000 --- a/weekly_tests/test03/test_short_swap.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/short_swap/files.ln/test_short_swap.c \ No newline at end of file diff --git a/weekly_tests/test04/Makefile b/weekly_tests/test04/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/weekly_tests/test04/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/weekly_tests/test04/bit_rotate b/weekly_tests/test04/bit_rotate deleted file mode 100755 index 3c59009..0000000 Binary files a/weekly_tests/test04/bit_rotate and /dev/null differ diff --git a/weekly_tests/test04/bit_rotate.c b/weekly_tests/test04/bit_rotate.c deleted file mode 100644 index bbea1fc..0000000 --- a/weekly_tests/test04/bit_rotate.c +++ /dev/null @@ -1,134 +0,0 @@ -#include "bit_rotate.h" - -// return the value bits rotated left n_rotations -uint16_t bit_rotate(int n_rotations, uint16_t bits) { - - uint16_t mask_left = bits & 0x8000; - uint16_t mask_right = bits & 0x0001; - int i = 0; - if (n_rotations > 0) { - while (i < n_rotations) { - mask_left = bits & 0x8000; - mask_left >>= 15; - bits <<= 1; - bits = bits | mask_left; - i++; - } - } else { - while (n_rotations < i) { - mask_right = bits & 0x0001; - mask_right <<= 15; - bits >>= 1; - bits |= mask_right; - n_rotations++; - } - } - - return bits; - - } - -// SAMPLE SOLUTION: - -/* -#include "bit_rotate.h" - -uint16_t bit_rotate(int n_rotations, uint16_t bits) { - uint32_t bits32 = bits; - n_rotations = n_rotations % 16; - if (n_rotations < 0) { - n_rotations += 16; - } - bits32 <<= n_rotations; - return (bits32 & 0xffff) | (bits32 >> 16); -} -*/ - - /* - if (bits == 0xffff) { - return 0xffff; - } - if (bits == 0x0000) { - return 0x0000; - } - - uint16_t rotated = 0; - uint16_t mask = 32768; // 1000 0000 0000 0000 - - //first check for negative - if (n_rotations < 0) { - uint16_t mask_flip = 1; - uint16_t flipped = 0; - for (int j = 0; j < 15; j++) { - flipped |= mask_flip & bits; - flipped <<= 1; - bits >>=1; - } - bits = flipped; - n_rotations = n_rotations * (-1); - n_rotations = n_rotations -1; - } - while (n_rotations > 15) { - n_rotations = n_rotations - 16; - } - for (int i = 0; i <= n_rotations; i++) { - - - rotated |= (mask & bits); - mask >>= 1; - } - - rotated >>= n_rotations; - bits <<= n_rotations; - rotated |= bits; - - - return rotated; - */ - - /* - uint16_t rotated = 0; - - - //first check for negative - if (n_rotations < 0) { - n_rotations = n_rotations * (-1); - uint16_t mask_right = 0; // 0000 0000 0000 0001 - for (int i = 0; i <= n_rotations; i++) { - - while (n_rotations > 15) { - n_rotations = n_rotations - 16; - } - rotated |= (mask_right & bits); - rotated <<= 1; - bits >>= 1; - } - printf("%x\n", rotated); - - rotated <<= (10); - rotated |= bits; - - } else { - uint16_t mask_left = 32768; // 1000 0000 0000 0000 - - for (int i = 0; i <= n_rotations; i++) { - - while (n_rotations > 15) { - n_rotations = n_rotations - 16; - } - rotated |= (mask_left & bits); - mask_left >>= 1; - } - - rotated >>= n_rotations; - bits <<= n_rotations; - rotated |= bits; - - } - - - return rotated; - - */ - - diff --git a/weekly_tests/test04/bit_rotate.h b/weekly_tests/test04/bit_rotate.h deleted file mode 120000 index 8dc2997..0000000 --- a/weekly_tests/test04/bit_rotate.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_rotate/files.ln/bit_rotate.h \ No newline at end of file diff --git a/weekly_tests/test04/bit_rotate.mk b/weekly_tests/test04/bit_rotate.mk deleted file mode 120000 index 17d8834..0000000 --- a/weekly_tests/test04/bit_rotate.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_rotate/files.ln/bit_rotate.mk \ No newline at end of file diff --git a/weekly_tests/test04/float_exp b/weekly_tests/test04/float_exp deleted file mode 100755 index 7a27df3..0000000 Binary files a/weekly_tests/test04/float_exp and /dev/null differ diff --git a/weekly_tests/test04/float_exp.c b/weekly_tests/test04/float_exp.c deleted file mode 100644 index 7aada6a..0000000 --- a/weekly_tests/test04/float_exp.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "float_exp.h" - -// given the 32 bits of a float return the exponent -uint32_t float_exp(uint32_t f) { - - //extract exponent (similar to float_bits in lab03) - uint32_t exponent = 0; - exponent = (f >> 23) & 0xff; - - return exponent; -} diff --git a/weekly_tests/test04/float_exp.h b/weekly_tests/test04/float_exp.h deleted file mode 120000 index 2676aed..0000000 --- a/weekly_tests/test04/float_exp.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_exp/files.ln/float_exp.h \ No newline at end of file diff --git a/weekly_tests/test04/float_exp.mk b/weekly_tests/test04/float_exp.mk deleted file mode 120000 index 869e7bb..0000000 --- a/weekly_tests/test04/float_exp.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_exp/files.ln/float_exp.mk \ No newline at end of file diff --git a/weekly_tests/test04/sign_flip b/weekly_tests/test04/sign_flip deleted file mode 100755 index aa105ad..0000000 Binary files a/weekly_tests/test04/sign_flip and /dev/null differ diff --git a/weekly_tests/test04/sign_flip.c b/weekly_tests/test04/sign_flip.c deleted file mode 100644 index 35e4765..0000000 --- a/weekly_tests/test04/sign_flip.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "sign_flip.h" - -// given the 32 bits of a float return it with its sign flipped -uint32_t sign_flip(uint32_t f) { - - // change sign of float - uint32_t sign = 0; - sign = f & 0x80000000; - - if (sign == 0) { - sign = 0x80000000; - } else { - sign = 0; - } - - // find f without sign - f &= 0x7fffffff; - - //join sign and new f into flipper float - uint32_t flipped = 0; - flipped = f | sign; - - return flipped; -} diff --git a/weekly_tests/test04/sign_flip.h b/weekly_tests/test04/sign_flip.h deleted file mode 120000 index d7c4ece..0000000 --- a/weekly_tests/test04/sign_flip.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/sign_flip/files.ln/sign_flip.h \ No newline at end of file diff --git a/weekly_tests/test04/sign_flip.mk b/weekly_tests/test04/sign_flip.mk deleted file mode 120000 index 53d3cfd..0000000 --- a/weekly_tests/test04/sign_flip.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/sign_flip/files.ln/sign_flip.mk \ No newline at end of file diff --git a/weekly_tests/test04/test_bit_rotate.c b/weekly_tests/test04/test_bit_rotate.c deleted file mode 120000 index 2ba2548..0000000 --- a/weekly_tests/test04/test_bit_rotate.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/bit_rotate/files.ln/test_bit_rotate.c \ No newline at end of file diff --git a/weekly_tests/test04/test_float_exp.c b/weekly_tests/test04/test_float_exp.c deleted file mode 120000 index 1dfc179..0000000 --- a/weekly_tests/test04/test_float_exp.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/float_exp/files.ln/test_float_exp.c \ No newline at end of file diff --git a/weekly_tests/test04/test_sign_flip.c b/weekly_tests/test04/test_sign_flip.c deleted file mode 120000 index 85de730..0000000 --- a/weekly_tests/test04/test_sign_flip.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/sign_flip/files.ln/test_sign_flip.c \ No newline at end of file diff --git a/weekly_tests/test05/min.c b/weekly_tests/test05/min.c deleted file mode 120000 index 0412293..0000000 --- a/weekly_tests/test05/min.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/min/files.ln/min.c \ No newline at end of file diff --git a/weekly_tests/test05/min.s b/weekly_tests/test05/min.s deleted file mode 100644 index e350d05..0000000 --- a/weekly_tests/test05/min.s +++ /dev/null @@ -1,44 +0,0 @@ -# Weekly Test 5 COMP1521 -# Written by Rifa Jamal - z5311190 - -# print the minimum of two integers -main: - li $v0, 5 # scanf("%d", &x); - syscall # - move $t0, $v0 - - li $v0, 5 # scanf("%d", &y); - syscall # - move $t1, $v0 - - - bge $t0, $t1, print_y # if x >= y then target - -print_x: - - move $a0, $t0 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j end - -print_y: - - move $a0, $t1 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - -end: - - li $v0, 0 # return 0 - jr $31 - diff --git a/weekly_tests/test05/not13.c b/weekly_tests/test05/not13.c deleted file mode 120000 index 2b4b326..0000000 --- a/weekly_tests/test05/not13.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/not13/files.ln/not13.c \ No newline at end of file diff --git a/weekly_tests/test05/not13.s b/weekly_tests/test05/not13.s deleted file mode 100644 index 5e3df36..0000000 --- a/weekly_tests/test05/not13.s +++ /dev/null @@ -1,43 +0,0 @@ -# Weekly Test 5 COMP1521 -# Written by Rifa Jamal - z5311190 - -main: - li $v0, 5 # scanf("%d", &x); - syscall # - move $t0, $v0 - - li $v0, 5 # scanf("%d", &y); - syscall # - move $t1, $v0 - - - - addi $t2, $t0, 1 # $t2 = $t0 + 1 - -loop_start: - - bge $t2, $t1, main_end # if $t2 >= $t1 main_endtarget - - beq $t2, 13, loop_increment # if $t2 == 13 then target - - move $a0, $t2 # printf("%d", x); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - -loop_increment: - - addi $t2, $t2, 1 # i++; - j loop_start - - -main_end: - - li $v0, 0 # return 0 - jr $31 - - diff --git a/weekly_tests/test05/square.c b/weekly_tests/test05/square.c deleted file mode 120000 index 2b49a34..0000000 --- a/weekly_tests/test05/square.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/square/files.ln/square.c \ No newline at end of file diff --git a/weekly_tests/test05/square.s b/weekly_tests/test05/square.s deleted file mode 100644 index ff45459..0000000 --- a/weekly_tests/test05/square.s +++ /dev/null @@ -1,42 +0,0 @@ -# Weekly Test 5 COMP1521 -# Written by Rifa Jamal - z5311190 - -main: - li $v0, 5 # scanf("%d", &x); - syscall # - move $t0, $v0 - - - li $t1, 0 # int i = 0 - -loop1: - - bge $t1, $t0, main_end # if x >= i then target - li $t2, 0 # int j = 0 - -loop2: - bge $t2, $t0, loop1_increment # if x >= j then target - - li $a0, '*' # printf("%c", '*'); - li $v0, 11 - syscall - -loop2_increment: - - addi $t2, $t2, 1 # j = j + 1 - j loop2 - -loop1_increment: - - addi $t1, $t1, 1 # i = 1 + 1; - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j loop1 - -main_end: - - li $v0, 0 # return 0 - jr $31 diff --git a/weekly_tests/test06/different10.c b/weekly_tests/test06/different10.c deleted file mode 120000 index 77d9fcd..0000000 --- a/weekly_tests/test06/different10.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/different10/files.ln/different10.c \ No newline at end of file diff --git a/weekly_tests/test06/different10.s b/weekly_tests/test06/different10.s deleted file mode 100644 index 4f5a9fe..0000000 --- a/weekly_tests/test06/different10.s +++ /dev/null @@ -1,77 +0,0 @@ -# Week 6 Weekly Test COMP1521 -# Written By Rifa Jamal (z5311190) - -# x in register $t0 -# i in register $t1 -# n_seen in register $t2 -# registers $t3 and $t4 used to hold temporary results -main: - - li $t2, 0 # n_seen = 0; -loop1_start: - bge $t2, 10, end # while (n_seen < 10) { - la $a0, string0 # printf("Enter a number: "); - li $v0, 4 - syscall - li $v0, 5 # scanf("%d", &x); - syscall - - move $t0, $v0 - - li $t1, 0 # int i = 0 -loop2_start: - bge $t1, $t2, if # if $t1 >= $t2 then target - - mul $t3, $t1, 4 # calculate &numbers[i] - la $t4, numbers # - add $t3, $t3, $t4 # - lw $t5, ($t3) # $t5 = numbers[i] - - beq $t0, $t5, if # if$t0 == $t1 then target - -loop2_increment: - addi $t1, $t1, 1 # $t1 = $t1 + 1 - j loop2_start - -if: - bne $t1, $t2, loop1_end # if != $t1 then target - - mul $t3, $t2, 4 # calculate &numbers[n_seen] - la $t4, numbers # - add $t3, $t3, $t4 # - sw $t0, ($t3) # numbers[n_seen] = x - -loop1_increment: - - add $t2, $t2, 1 # n_seen++; - -loop1_end: - - b loop1_start - - -end: - la $a0, string1 # printf("10th different number was: "); - li $v0, 4 - syscall - - move $a0, $t0 # printf("%d", x) - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - li $v0, 0 # return 0 - jr $31 - - .data - -numbers: - .space 40 # int numbers[10]; - -string0: - .asciiz "Enter a number: " -string1: - .asciiz "10th different number was: " diff --git a/weekly_tests/test06/not_negative.c b/weekly_tests/test06/not_negative.c deleted file mode 120000 index 5be2851..0000000 --- a/weekly_tests/test06/not_negative.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/not_negative/files.ln/not_negative.c \ No newline at end of file diff --git a/weekly_tests/test06/not_negative.s b/weekly_tests/test06/not_negative.s deleted file mode 100644 index 81c6945..0000000 --- a/weekly_tests/test06/not_negative.s +++ /dev/null @@ -1,49 +0,0 @@ -# Week 6 Weekly Test COMP1521 -# Written By Rifa Jamal (z5311190) - -# read numbers until a non-negative number entered -# x in $t0 -main: - - la $a0, str0 # printf("Enter a number: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d", &x); - syscall # - move $t0, $v0 - -if: - - bge $t0, 0, else # if $t0 >= 0 then target - - la $a0, str2 # printf("Enter a positive number"); - li $v0, 4 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j main - -else: - - la $a0, str1 # printf("You entered: "); - li $v0, 4 - syscall - move $a0, $t0 # printf("%d", x); - li $v0, 1 - syscall - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - li $v0, 0 # return 0 - jr $31 - -.data -str0: - .asciiz "Enter a number: " -str1: - .asciiz "You entered: " -str2: - .asciiz "Enter a positive number" diff --git a/weekly_tests/test06/reverse_negative.c b/weekly_tests/test06/reverse_negative.c deleted file mode 120000 index ebb8221..0000000 --- a/weekly_tests/test06/reverse_negative.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/reverse_negative/files.ln/reverse_negative.c \ No newline at end of file diff --git a/weekly_tests/test06/reverse_negative.s b/weekly_tests/test06/reverse_negative.s deleted file mode 100644 index d28a8de..0000000 --- a/weekly_tests/test06/reverse_negative.s +++ /dev/null @@ -1,58 +0,0 @@ -# Week 6 Weekly Test COMP1521 -# Written By Rifa Jamal (z5311190) - -# Read numbers into an array until a negative number is entered -# then print the numbers in reverse order - -# i in register $t0 -# registers $t1, $t2 & $t3 used to hold temporary results - -main: - li $t0, 0 # i = 0 -loop0: - bge $t0, 1000, loop1 # while (i < 1000) { - - li $v0, 5 # scanf("%d", &x); - syscall # - move $t1, $v0 # int x - -if: - bge $t1, 0, else # if $t1 >= 0 then target - j loop1 - -else: - mul $t2, $t0, 4 # calculate &numbers[i] - la $t3, numbers # - add $t4, $t2, $t3 # - sw $t1, ($t4) # store entered number in array - - addi $t0, $t0, 1 # i++; - b loop0 # } - - -loop1: - ble $t0, 0, end # if i <= 0 then target - addi $t0, $t0, -1 # i--; - - mul $t2, $t0, 4 # calculate &numbers[i] - la $t3, numbers # - add $t4, $t2, $t3 # - lw $a0, ($t4) # load word stored in numbers[i] - - li $v0, 1 # printf("%d", numbers[i]); - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - j loop1 - -end: - - li $v0, 0 # return 0 - jr $31 # - -.data -numbers: - .space 4000 diff --git a/weekly_tests/test07/line_char.c b/weekly_tests/test07/line_char.c deleted file mode 120000 index b3c7e3b..0000000 --- a/weekly_tests/test07/line_char.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/line_char/files.ln/line_char.c \ No newline at end of file diff --git a/weekly_tests/test07/line_char.s b/weekly_tests/test07/line_char.s deleted file mode 100644 index b4fc7a1..0000000 --- a/weekly_tests/test07/line_char.s +++ /dev/null @@ -1,58 +0,0 @@ -# Weekly Test 7 COMP1521 -# Written by Rifa Jamal - z5311190 -# Written on 23/07/2020 - -# read a line from stdin and and then an integer n -# Print the character in the nth-position - -# n in $t0 - -main: - la $a0, str0 # printf("Enter a line of input: "); - li $v0, 4 - syscall - - la $a0, line # fgets(buffer, 256, stdin) - la $a1, 256 - li $v0, 8 - syscall - - la $a0, str1 # printf("Enter a position: "); - li $v0, 4 - syscall - - li $v0, 5 # scanf("%d"); - syscall - move $t0, $v0 - - la $a0, str2 # printf("Character is: "); - li $v0, 4 - syscall - - la $t1, line # find memory address of line - add $t2, $t0, $t1 # add row position to line address to find row offset - - lb $a0, ($t2) # load address of line[n] - li $v0, 11 # printf("%c", '?'); - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - li $v0, 0 # return 0 - jr $31 - -.data -str0: - .asciiz "Enter a line of input: " -str1: - .asciiz "Enter a position: " -str2: - .asciiz "Character is: " - - -# line of input stored here -line: - .space 256 - diff --git a/weekly_tests/test07/line_length.c b/weekly_tests/test07/line_length.c deleted file mode 120000 index 4d11f02..0000000 --- a/weekly_tests/test07/line_length.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/line_length/files.ln/line_length.c \ No newline at end of file diff --git a/weekly_tests/test07/line_length.s b/weekly_tests/test07/line_length.s deleted file mode 100644 index ed7682b..0000000 --- a/weekly_tests/test07/line_length.s +++ /dev/null @@ -1,58 +0,0 @@ -# Weekly Test 7 COMP1521 -# Written by Rifa Jamal - z5311190 -# Written on 23/07/2020 - -# read a line and print its length - -main: - la $a0, str0 # printf("Enter a line of input: "); - li $v0, 4 - syscall - - la $a0, line - la $a1, 256 - li $v0, 8 # fgets(buffer, 256, stdin) - syscall # - - li $t0, 0 # int i = 0; - -loop_start: - - la $t1, line - add $t2, $t0, $t1 - lb $t3, ($t2) - - beq $t3, 0, loop_end # if line[i] == 0 then loop_end - - addi $t0, $t0, 1 # i++; - b loop_start - -loop_end: - - la $a0, str1 # printf("Line length: "); - li $v0, 4 - syscall - - move $a0, $t0 # printf("%d", i); - li $v0, 1 - syscall - - li $a0, '\n' # printf("%c", '\n'); - li $v0, 11 - syscall - - li $v0, 0 # return 0 - jr $31 - - -.data -str0: - .asciiz "Enter a line of input: " -str1: - .asciiz "Line length: " - - -# line of input stored here -line: - .space 256 - diff --git a/weekly_tests/test07/palindrome.c b/weekly_tests/test07/palindrome.c deleted file mode 120000 index 306ac83..0000000 --- a/weekly_tests/test07/palindrome.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/palindrome/files.ln/palindrome.c \ No newline at end of file diff --git a/weekly_tests/test07/palindrome.s b/weekly_tests/test07/palindrome.s deleted file mode 100644 index d911e70..0000000 --- a/weekly_tests/test07/palindrome.s +++ /dev/null @@ -1,87 +0,0 @@ -# Weekly Test 7 COMP1521 -# Written by Rifa Jamal - z5311190 -# Written on 23/07/2020 - -# read a line and print whether it is a palindrom - -# int i in $s0 - -main: - la $a0, str0 # printf("Enter a line of input: "); - li $v0, 4 - syscall - - la $a0, line - la $a1, 256 - li $v0, 8 # fgets(buffer, 256, stdin) - syscall - - - li $s0, 0 # int i = 0; - -loop1: - - la $t0, line - add $t1, $s0, $t0 - lb $t2, ($t1) - - beq $t2, 0, loop1_end # if line[i] == 0 then loop_end - - addi $s0, $s0, 1 # i++; - b loop1 - -loop1_end: - - li $s1, 0 # int j = 0; - addi $s2, $s0, -2 # int k = i - 2; - -loop2: - - bge $s1, $s2, loop2_end # if j >= k then loop2_end - - la $t0, line - add $t1, $s1, $t0 - lb $t2, ($t1) # $t2 = line[j] - - la $t0, line - add $t1, $s2, $t0 - lb $t3, ($t1) # $t2 = line[k] - - beq $t2, $t3, loop2_increment # if j == k then target - - la $a0, not_palindrome - li $v0, 4 - syscall - - li $v0, 0 # return 0 - jr $31 - -loop2_increment: - - addi $s1, $s1, 1 # j++; - addi $s2, $s2, -1 # k++; - b loop2 - -loop2_end: - - la $a0, palindrome - li $v0, 4 - syscall - - li $v0, 0 # return 0 - jr $31 - - -.data -str0: - .asciiz "Enter a line of input: " -palindrome: - .asciiz "palindrome\n" -not_palindrome: - .asciiz "not palindrome\n" - - -# line of input stored here -line: - .space 256 - diff --git a/weekly_tests/test08/compare_file b/weekly_tests/test08/compare_file deleted file mode 100755 index ef00006..0000000 Binary files a/weekly_tests/test08/compare_file and /dev/null differ diff --git a/weekly_tests/test08/compare_file.c b/weekly_tests/test08/compare_file.c deleted file mode 100644 index 68416bc..0000000 --- a/weekly_tests/test08/compare_file.c +++ /dev/null @@ -1,126 +0,0 @@ -// Weekley Test 8 -// Written by Rifa Jamal z5311190 - -// compare_file.c, which takes two arguments, both filenames. Compare_file.c should print one line of output. -// If the two files are different compare_file.c should print the location of the first byte whether they differ. -// If the one file is shorter than the other but the bytes it contains are identical to the other file, -// compare_file.c should print a message indicating this. Use the same format as the example below. -// If the 2 files contain exactly the same bytes compare_file.c should print a message indicating this. - -#include -#include - -int main(int argc, char *argv[]) { - - //check if only 2 files entered/ 3 arguments - if (argc != 3) { - fprintf(stderr, "Enter two files only\n"); - return 1; - } - - FILE *file1 = fopen(argv[1], "r"); - - if (file1 == NULL) { - perror(argv[1]); - exit(1); - } - - FILE *file2 = fopen(argv[2], "r"); - - if (file2 == NULL) { - perror(argv[2]); - exit(1); - } - - int pos = 0; - int i = 0; - int byte1 = fgetc(file1); - int byte2 = fgetc(file2); - - while (byte1 != EOF && byte2 != EOF && i == 0) { - if (byte1 != byte2) { - printf("Files differ at byte %d\n", pos); - i++; - } - pos++; - byte1 = fgetc(file1); - byte2 = fgetc(file2); - } - - if ((byte1 == EOF) && byte2 != EOF) { - printf("EOF on %s\n", argv[1]); - return 0; - } else if (byte1 != EOF && byte2 == EOF) { - printf("EOF on %s\n", argv[2]); - return 0; - } else if (i == 0) { - printf("Files are identical\n"); - } - - fclose(file1); - fclose(file2); - return 0; - -} - - -// SAMPLE SOLUTIONNNN - -/* - -// Print position of first non-ASCII byte in file - -#include -#include - -int compare_files(char *pathname1, char *pathname2); - -int main(int argc, char *argv[]) { - if (argc != 3) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return 1; - } - - return compare_files(argv[1], argv[2]); -} - -int compare_files(char *pathname1, char *pathname2) { - FILE *stream1 = fopen(pathname1, "r"); - if (stream1 == NULL) { - perror(pathname1); - return 1; - } - - FILE *stream2 = fopen(pathname2, "r"); - if (stream2 == NULL) { - perror(pathname2); - return 1; - } - - ssize_t byte_position = 0; - int byte1, byte2; - while (1) { - byte1 = fgetc(stream1); - byte2 = fgetc(stream2); - if (byte1 != byte2 || byte1 == EOF) { - break; - } - byte_position++; - } - - if (byte1 == byte2) { - printf("Files are identical\n"); - } else if (byte1 == EOF) { - printf("EOF on %s\n", pathname1); - } else if (byte2 == EOF) { - printf("EOF on %s\n", pathname2); - } else { - printf("Files differ at byte %zd\n", byte_position); - } - - fclose(stream1); - fclose(stream2); - return 0; -} - -*/ \ No newline at end of file diff --git a/weekly_tests/test08/file b/weekly_tests/test08/file deleted file mode 100644 index 3b18e51..0000000 --- a/weekly_tests/test08/file +++ /dev/null @@ -1 +0,0 @@ -hello world diff --git a/weekly_tests/test08/file1 b/weekly_tests/test08/file1 deleted file mode 100644 index 3b18e51..0000000 --- a/weekly_tests/test08/file1 +++ /dev/null @@ -1 +0,0 @@ -hello world diff --git a/weekly_tests/test08/file2 b/weekly_tests/test08/file2 deleted file mode 100644 index e69de29..0000000 diff --git a/weekly_tests/test08/file3 b/weekly_tests/test08/file3 deleted file mode 100644 index b6fc4c6..0000000 --- a/weekly_tests/test08/file3 +++ /dev/null @@ -1 +0,0 @@ -hello \ No newline at end of file diff --git a/weekly_tests/test08/file4 b/weekly_tests/test08/file4 deleted file mode 100644 index e48188d..0000000 --- a/weekly_tests/test08/file4 +++ /dev/null @@ -1 +0,0 @@ -help me diff --git a/weekly_tests/test08/leave_only_ascii b/weekly_tests/test08/leave_only_ascii deleted file mode 100755 index e3c9203..0000000 Binary files a/weekly_tests/test08/leave_only_ascii and /dev/null differ diff --git a/weekly_tests/test08/leave_only_ascii.c b/weekly_tests/test08/leave_only_ascii.c deleted file mode 100644 index 4fd5ee2..0000000 --- a/weekly_tests/test08/leave_only_ascii.c +++ /dev/null @@ -1,79 +0,0 @@ -// Weekley Test 8 -// Written by Rifa Jamal z5311190 - -// leave_only_ascii.c, which takes one argument, a filename. -// leave_only_ascii.c should remove all non-ASCII bytes from the file. -// After it is run the file should contain only a ASCII bytes. -// It should print nothing on stdout. It should only change the file. -// Assume a byte is non-ASCII if it contains a value between 128..255 inclusive. - -#include -#include -#include - -int main(int argc, char *argv[]) { - - /* - FILE *input = fopen(argv[1], "r"); - - if (input == NULL) { - perror(argv[1]); - exit(1); - } - - char temp[strlen(argv[1])+1]; - strcpy(temp, argv[1]); - - FILE *output = fopen("temp", "w"); - if (output == NULL) { - perror(argv[1]); - exit(1); - } - - int byte; - while ((byte = fgetc(input)) != EOF) { - if (byte <= 127) { - fputc(byte, output); - } - } - fclose(input); - fclose(output); - - //remove temporary file - remove("temp"); - */ - - FILE *output = fopen(argv[1], "r"); - - if (output == NULL) { - perror(argv[1]); - exit(1); - } - - FILE *temp = fopen("temp", "w"); - - int byte; - while ((byte = fgetc(output)) != EOF) { - if (byte <= 127) { - fputc(byte, temp); - } - } - - fclose(output); - fclose(temp); - - FILE *change_file = fopen(argv[1], "w"); - FILE *read_temp = fopen("temp", "r"); - - while ((byte =fgetc(read_temp)) != EOF) { - fputc(byte, change_file); - } - - fclose(read_temp); - fclose(change_file); - remove("temp"); - - - return 0; - -} \ No newline at end of file diff --git a/weekly_tests/test08/non_ascii b/weekly_tests/test08/non_ascii deleted file mode 100755 index 0226820..0000000 Binary files a/weekly_tests/test08/non_ascii and /dev/null differ diff --git a/weekly_tests/test08/non_ascii.c b/weekly_tests/test08/non_ascii.c deleted file mode 100644 index 9774ba3..0000000 --- a/weekly_tests/test08/non_ascii.c +++ /dev/null @@ -1,37 +0,0 @@ -// Weekley Test 8 -// Written by Rifa Jamal z5311190 - -// non_ascii.c, which takes one argument, a filename. -// It should print one line of output. -// If the file contains a non-ASCII byte, non_ascii.c should print the location of the first non-ASCII byte. Use the same format as the example below. -// If the file contains no non-ASCII byte non_ascii.c should print a message indicating this. -// Assume a byte is non-ASCII if it contains a value between 128..255 inclusive. - -#include -#include - -int main(int argc, char *argv[]) { - - FILE *outfile = fopen(argv[1], "r"); - - if (outfile == NULL) { - //perror(argv[1]); -> both are same - perror(argv[3]); - exit(1); - } - - int pos = 0; - int byte; - - while ((byte = fgetc(outfile)) != EOF){ - if (byte >= 128 && byte <= 255) { - printf("%s: byte %d is non-ASCII\n", argv[1], pos); - return 0; - } - pos++; - } - - fclose(outfile); - printf("%s is all ASCII\n", argv[1]); - return 0; -} \ No newline at end of file diff --git a/weekly_tests/test08/test1 b/weekly_tests/test08/test1 deleted file mode 100644 index e69de29..0000000 diff --git a/weekly_tests/test09/Makefile b/weekly_tests/test09/Makefile deleted file mode 120000 index 851b44b..0000000 --- a/weekly_tests/test09/Makefile +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/resources/Makefile.exercises \ No newline at end of file diff --git a/weekly_tests/test09/add b/weekly_tests/test09/add deleted file mode 100755 index c19658b..0000000 Binary files a/weekly_tests/test09/add and /dev/null differ diff --git a/weekly_tests/test09/add.c b/weekly_tests/test09/add.c deleted file mode 100644 index 2a6b0bb..0000000 --- a/weekly_tests/test09/add.c +++ /dev/null @@ -1,18 +0,0 @@ -//Weekly Test09 -//COMP1521 - Rifa Jamal z5311190 -//Written on 6/08/2020 - -#include -#include -#include - -#include "add.h" - -// return the MIPS opcode for add $d, $s, $t -uint32_t add(uint32_t d, uint32_t s, uint32_t t) { - - uint32_t funct = 1; - uint32_t opcode = (s << 21)|(t << 16)|(d << 11)|(funct << 5); - return opcode; - -} diff --git a/weekly_tests/test09/add.h b/weekly_tests/test09/add.h deleted file mode 120000 index 854ed7e..0000000 --- a/weekly_tests/test09/add.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/add/files.ln/add.h \ No newline at end of file diff --git a/weekly_tests/test09/add.mk b/weekly_tests/test09/add.mk deleted file mode 120000 index e839645..0000000 --- a/weekly_tests/test09/add.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/add/files.ln/add.mk \ No newline at end of file diff --git a/weekly_tests/test09/get_string b/weekly_tests/test09/get_string deleted file mode 100755 index 6a68dbc..0000000 Binary files a/weekly_tests/test09/get_string and /dev/null differ diff --git a/weekly_tests/test09/get_string.c b/weekly_tests/test09/get_string.c deleted file mode 100644 index 6d465c7..0000000 --- a/weekly_tests/test09/get_string.c +++ /dev/null @@ -1,80 +0,0 @@ -//Weekly Test09 -//COMP1521 - Rifa Jamal z5311190 -//Written on 6/08/2020 - -#include - -#include "get_string.h" - -// print a line from stream using fgetc (only) -// reads in at most one less than size characters from stream and stores them into the -// buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is -// stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer. -void get_string(char *s, int size, FILE *stream) { - - -// SAMPLE SOLUTION - MUCH BETTER: -// HAVENT COMMENTED FUNCTION OUTTTTTT - -void get_string(char *s, int size, FILE *stream) { - - int bytes_read = 0; - int c; - while (bytes_read < size - 1 && (c = fgetc(stream)) != EOF) { - s[bytes_read] = c; - bytes_read++; - if (c == '\n') { - break; - } - } - s[bytes_read] = '\0'; - -} - - /* - int i; - //ch = fgetc(stream); - i = 0; - // Read in size - 1 characters and place them in "s": - while (fgetc(stdin) != EOF) { - while (i < (size - 1)){ - //ch = fgetc(stream); - //s[i] = (char)ch; - s[i] = *s; - //s++; - i++; - } - } - // Add null to end string - s[i] = '\0'; - - //printf( "%s\n", s); - */ - int i = 0; - int ch; - while((ch = fgetc(stdin)) != '\n' && (i < (size -1))) { - if(ch == EOF) { - break; - } - s[i] = ch; - i++; - - } - if (i == size - 1) { - - s[size - 1] = '\0'; - - } else { - - if (ch == '\n') { - s[i] = '\n'; - } - - // the strings should be end with '\0' - s[i+1] = '\0'; - } - - -} - - diff --git a/weekly_tests/test09/get_string.h b/weekly_tests/test09/get_string.h deleted file mode 120000 index 6f48c19..0000000 --- a/weekly_tests/test09/get_string.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/get_string/files.ln/get_string.h \ No newline at end of file diff --git a/weekly_tests/test09/get_string.mk b/weekly_tests/test09/get_string.mk deleted file mode 120000 index 3ca0906..0000000 --- a/weekly_tests/test09/get_string.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/get_string/files.ln/get_string.mk \ No newline at end of file diff --git a/weekly_tests/test09/put_string b/weekly_tests/test09/put_string deleted file mode 100755 index 11c386f..0000000 Binary files a/weekly_tests/test09/put_string and /dev/null differ diff --git a/weekly_tests/test09/put_string.c b/weekly_tests/test09/put_string.c deleted file mode 100644 index 798baf0..0000000 --- a/weekly_tests/test09/put_string.c +++ /dev/null @@ -1,20 +0,0 @@ -//Weekly Test09 -//COMP1521 - Rifa Jamal z5311190 -//Written on 6/08/2020 - -#include - -#include "put_string.h" - -// print s to stdout with a new line appended using fputc (only) - -void put_string(char *s) { - - while (*s != '\0') { - fputc(*s, stdout); - s++; - } - - fputc('\n', stdout); - -} diff --git a/weekly_tests/test09/put_string.h b/weekly_tests/test09/put_string.h deleted file mode 120000 index 70aa303..0000000 --- a/weekly_tests/test09/put_string.h +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/put_string/files.ln/put_string.h \ No newline at end of file diff --git a/weekly_tests/test09/put_string.mk b/weekly_tests/test09/put_string.mk deleted file mode 120000 index 7d96340..0000000 --- a/weekly_tests/test09/put_string.mk +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/put_string/files.ln/put_string.mk \ No newline at end of file diff --git a/weekly_tests/test09/test_add.c b/weekly_tests/test09/test_add.c deleted file mode 120000 index e96f181..0000000 --- a/weekly_tests/test09/test_add.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/add/files.ln/test_add.c \ No newline at end of file diff --git a/weekly_tests/test09/test_get_string.c b/weekly_tests/test09/test_get_string.c deleted file mode 120000 index d5c24b5..0000000 --- a/weekly_tests/test09/test_get_string.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/get_string/files.ln/test_get_string.c \ No newline at end of file diff --git a/weekly_tests/test09/test_put_string.c b/weekly_tests/test09/test_put_string.c deleted file mode 120000 index b5b5aba..0000000 --- a/weekly_tests/test09/test_put_string.c +++ /dev/null @@ -1 +0,0 @@ -/web/cs1521/20T2/activities/put_string/files.ln/test_put_string.c \ No newline at end of file diff --git a/weekly_tests/test10/Makefile b/weekly_tests/test10/Makefile deleted file mode 100644 index bed87f5..0000000 --- a/weekly_tests/test10/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# COMP1521 20T2 ... a general makefile for multiple exercises - -CC = dcc -EXERCISES ?= -CLEAN_FILES ?= - -.DEFAULT_GOAL = all -.PHONY: all - --include *.mk - -all: ${EXERCISES} - -clean: - -rm -f ${CLEAN_FILES} \ No newline at end of file diff --git a/weekly_tests/test10/is_diary_public b/weekly_tests/test10/is_diary_public deleted file mode 100755 index db00623..0000000 Binary files a/weekly_tests/test10/is_diary_public and /dev/null differ diff --git a/weekly_tests/test10/is_diary_public.c b/weekly_tests/test10/is_diary_public.c deleted file mode 100644 index c648f3b..0000000 --- a/weekly_tests/test10/is_diary_public.c +++ /dev/null @@ -1,42 +0,0 @@ -// Weekly Test 10 COMP1521 -// Written by Rifa Jamal z5311190 -// On 13/08/2020 - -// is_diary_public.c, which prints 1 if a file named -// $HOME/.diary exists and is publically readable. -// It should print 0 otherwise. -// $HOME means the value of the environment variable HOME. - -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char *argv[]) { - - char *home = getenv("HOME"); - - int size = snprintf(NULL, 0, "%s/.diary", home); - char *path = calloc(size + 1, sizeof(char)); - snprintf(path, size + 1, "%s/.diary", home); - - struct stat s; - - /*if (stat(path, &s) < 0) { - perror(path); - return 1; - }*/ - - if((stat(path, &s) == 0) && (s.st_mode & S_IROTH)){ - printf("1\n"); - - } else { - printf("0\n"); - } - - return 0; - -} \ No newline at end of file diff --git a/weekly_tests/test10/is_directory b/weekly_tests/test10/is_directory deleted file mode 100755 index 284975f..0000000 Binary files a/weekly_tests/test10/is_directory and /dev/null differ diff --git a/weekly_tests/test10/is_directory.c b/weekly_tests/test10/is_directory.c deleted file mode 100644 index a121283..0000000 --- a/weekly_tests/test10/is_directory.c +++ /dev/null @@ -1,41 +0,0 @@ -// Weekly Test 10 COMP1521 -// Written by Rifa Jamal z5311190 -// On 13/08/2020 - -// is_directory.c, which takes one argument a pathname. -// If the pathname exists and is a directory, -// it should print 1, otherwise it should print 0. - -#include -#include -#include -#include -#include - -int main(int argc, char *argv[]) { - - assert(argc == 2); - char *path = argv[1]; - - struct stat s; - if (stat(path, &s) == 0) { - if( s.st_mode & S_IFDIR ) { - //it's a directory - printf("1\n"); - } - /*else if( s.st_mode & S_IFREG ) { - //it's a file - printf("0\n"); - }*/ - else { - //something else - printf("0\n"); - } - } - else { - //error - printf("0\n"); - } - - return 0; -} \ No newline at end of file diff --git a/weekly_tests/test10/is_var_set b/weekly_tests/test10/is_var_set deleted file mode 100755 index b190b63..0000000 Binary files a/weekly_tests/test10/is_var_set and /dev/null differ diff --git a/weekly_tests/test10/is_var_set.c b/weekly_tests/test10/is_var_set.c deleted file mode 100644 index b7112dd..0000000 --- a/weekly_tests/test10/is_var_set.c +++ /dev/null @@ -1,26 +0,0 @@ -// Weekly Test 10 COMP1521 -// Written by Rifa Jamal z5311190 -// On 13/08/2020 - -// is_var_set.c, which takes one argument, the name of environment variable -// If the environment variable is set to a non-empty string, -// it should print 1, otherwise it should print 0. - -#include -#include -#include -#include - -int main(int argc, char *argv[]) { - - assert(argc == 2); - char *stream = argv[1]; - char *env_var = getenv(stream); - if (env_var != NULL && env_var[0] != '\0') { - printf("1\n"); - } else { - printf("0\n"); - } - return 0; - -} diff --git a/weekly_tests/test10/test_file b/weekly_tests/test10/test_file deleted file mode 100644 index 8b13789..0000000 --- a/weekly_tests/test10/test_file +++ /dev/null @@ -1 +0,0 @@ -