Assignment 2 Solution

SAL program

# This SAL program checks for the divisibility of the entered 
# integer by 3,4 and 5. The focus of this assignment is on the use of
# procedures with proper error checking and appropriate documentation.

# Data Definitions

.data

# Return addresses for the procedures indicated by their side

get_int_ra: .word # procedure get_int divisible_ra: .word # procedure divisible print_ra: .word # procedure print

int .word # entered integer divisor .word # current divisor of integer min_div: .word 3 # least value of divisor (for the loop) max_div: .word 5 # maximum value of divisor (for the loop) reminder: .word # holds the reminder after division rflag: .word # reminder flag --> 0 indicates divisible # 1 indicates not divisible

# Some character and string constants to provide nice user interface

newline: .byte '\n'

enter_str: .asciiz "\nEnter a positive integer(0 to quit): " neg_str : .asciiz "\nSorry! Try with a +ve integer\n " bye_str: .asciiz "\nBye Bye! Hope u enjoyed this \n\n" div_str: .asciiz " is evenly divisible by " ndiv_str: .asciiz " is not evenly divisible by "

# Code # This program inputs an integer, prints if it is evenly divisible by # 3,4 and 5 and repeats until the user enters 0. For negative integers # an error message is given

.text __start: la get_int_ra,rtn1 # load return address for proc get_int b get_int rtn1: blez int,wrong # go to wrong if the integer is <= 0 move divisor,min_div # divisor is initialised to min_div(loop) divide_loop: la divisible_ra,increment # load retaddr for proc divisible b divisible increment: add divisor,divisor,1 # increment divisor ble divisor,max_div,divide_loop # if divisor <= max_div loop b __start # go back to the repeat loop wrong: beqz int,end # check if int = 0 (--> quit) puts neg_str # int is negative b __start end: puts bye_str # end of program done

# get_int inputs an integer

get_int: puts enter_str get int b (get_int_ra)

# procedure divisible checks if int is evenly divisible by divisor

divisible: move rflag,0 # initialise rflag to 0 rem reminder,int,divisor # reminder= int mod divisor beqz reminder,call_print # if reminder=0 call print proc move rflag,1 # else set rflag and call print call_print: la print_ra,div_ret # loading return addr for print b print div_ret: b (divisible_ra)

# procedure print checks if reminder flag is set or not. # If reset, it prints evenly divisible # If set, it prints not divisible

print: put newline put int beqz rflag,print_div puts ndiv_str # not divisible put divisor put newline b print_ret print_div: puts div_str # divisible put divisor put newline print_ret: b (print_ra)

Grading Criteria

For this assignment, Documentation, Error checking and coding style have been taken into account besides a working code. Following is the relative importance of the categories :

Important piece of information

For this and all future assignments, the first copy that we come across will be graded. So, make sure that you go over your work before handing it in.