

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


#################################################################################
################################################################################# .data test: .word 0x32111113, 0x .ascii "+" string: space 64 bcd1: .space 32 bcd2: .space 32 bcdsum: .word 8 .text main: la $a0, string # $a0 = address of input buffer li $a1, 64 # $a1 = size of input buffer li $v0, 8 # system call code for reading first decimal string syscall la $a1, bcd jal tobcd # convert to bcd la $a0, string # $a0 = address of input buffer li $a1, 64 # $a1 = size of input buffer li $v0, 8 # system call code for reading second decimal string syscall la $a1, bcd jal tobcd #convert to bcd la $a0, bcd la $a1, bcd la $a3, bcdsum
la $a0, string la $a1, bcdsum
la $a0, string li $v0, 4 # system call code for print string syscall li $v0, 10 # system call code for exit syscall
#################################################################################
################################################################################# tobcd: li $t7, '\n' li $t9, '-' move $t0, $a move $t1, $a l2: lb $t2, 0($t0) beq $t2, $t7, eos addiu $t0, $t0, 1 j l eos: sub $t3, $t0, $a0 # $t3 = length of string (incl sign) addi $t0, $t0, -1 # move char point back to rightmost char lb $t2, 0($t0) # load least signficant digit addi $t2, $t2, -48 # convert to nibble sll $t2, $t2, 4 # shift left 4 bit to make room for numwords/sign sw $zero, 0($t1) # zero out bcd buffer sw $zero, 4($t1) sw $zero, 8($t1) sw $zero, 12($t1) sw $zero, 16($t1) sw $zero, 20($t1) sw $zero, 24($t1) l3: sb $t2, 0($t1) # store buffer byte addi $t1, $t1, 1 # move to next byte in bcd buffer addi $t0, $t0, -1 # move char pointer to left lb $t2, 0($t0) # load even digit (from right) addi $t2, $t2, -48 # convert to nibble bltz $t2, done # make sure char is not sign addi $t0, $t0, -1 # move char pointer to left lb $t4, 0($t0) # load odd digit (from right) addi $t4, $t4, -48 # converto to nibble bltz $t4, done # make sure char is not sign sll $t4, $t4, 4 # shift odd nibble 4 bits to left add $t2, $t2, $t4 # concatenate two nibble j l done: bne $t0, $a0, nosign # check if string started with a sign char addi $t3, $t3, -1 # if so, adjust digit count nosign: srl $t3, $t3, 3 # divide digit count by 8 to get # additional words sll $t3, $t3, 1 # move this number to left 1 bit to make room for sign bit lb $t2, 0($t0) bne $t2, $t9, notneg addi $t3, $t3, 1 # set the sign bit notneg: lb $t2, 0($a1) # reload first byte of buffer (least significant nibble << 4) add $t2, $t2, $t3 # add numwords/sign sb $t2, 0($a1) # store in buffer jr $ra