#define c_print_int 1 #define c_print_str 4 #define c_atoi 5 #define c_sbrk 9 #define c_exit 10 #define c_print_char 11 #define c_exit2 17 .data error_msg : .asciiz "\nProgram expects two arguments. \n Usage: main.s input0 input1 [1:Add,0:Mul] \n" i0 : .asciiz "\nInput0 :" i1 : .asciiz "\nInput1 :" res : .asciiz "\nResult :" .globl main .text #================================================================ # void print_int(int a1) # Prints the integer in a1. # args: # a1 = integer to print # return: # void #================================================================ print_int: li a0 c_print_int ecall jr ra #================================================================ # void print_str(char *a1) # Prints the null-terminated string at address a1. # args: # a1 = address of the string you want printed. # return: # void #================================================================ print_str: li a0 c_print_str ecall jr ra #================================================================ # int atoi(char* a1) # Returns the integer version of the string at address a1. # args: # a1 = address of the string you want to turn into an integer. # return: # a0 = Integer representation of string #================================================================ atoi: li a0 c_atoi ecall jr ra #================================================================ # void print_char(char a1) # Prints the ASCII character in a1 to the console. # args: # a1 = character to print # return: # void #================================================================ print_char: li a0 c_print_char ecall jr ra #================================================================ # void noreturn exit() # Exits the program with a zero exit code. # args: # None # return: # No Return #================================================================ exit: li a0 c_exit ecall #================================================================ # void noreturn exit2(int a1) # Exits the program with error code a1. # args: # a1 = Exit code. # return: # This program does not return. #================================================================ exit2: li a0 c_exit2 ecall jr ra .text main: # ===================================== # COMMAND LINE ARGUMENTS # ===================================== # Args: # a0: int argc # a1: char** argv # # Exit if incorrect number of command line args li s2, 3 # Expect 3 arguments mv s3, a0 # (argc) blt a0, s2, error # if argc < 3 jump to exit # argv[1] mv s4, a1 # (char** argv). s4 char* argv[] # 1st Argument addi s4,s4,4 # Dereferencing the first string (Why is this required ? ) lw a1, 0 (s4) # s4[0]. (pointer to string containing argument 0 value) jal atoi # (convert string to integer) mv s0,a0 # (store return result in s0) # 2nd argument addi s4,s4,4 lw a1, 0(s4) # (pointer to string containing argument 1 value) jal atoi # (convert string to integer) mv s1,a0 # (store return result in s0) # 3rd argument addi s4,s4,4 lw a1, 0(s4) jal atoi mv s2,a0 # S0 - argument 0, S1 - argument 1, S2 - 1 - do + 0 - do * beq s2,zero, multiply # Jump to multiply routine add s3,s0,s1 # add routine j exit_prog # jump over multiply to exit multiply: mul s3,s0,s1 exit_prog: la a1, i0 # Print str jal print_str mv a1,s0 # Print value jal print_int la a1,i1 jal print_str mv a1,s1 jal print_int la a1,res jal print_str mv a1,s3 jal print_int # Print newline afterwards for clarity li a1 '\n' jal print_char j exit error: la a1, error_msg jal print_str li a1, 1 jal exit2