################################################################################ # MIPS assembly implementation of an unsigned division algorithm that will # # divide a 32 bit integer by another 32 bit integer to calculate the quotient # # and remainder # ################################################################################ # Optimized for the standard terminal size of 80 columns x 24 rows # ################################################################################ .data # the data segment to store global data dividz: .asciiz "Dividend: " # /* divisz: .asciiz "Divisor : " # no ergebz: .asciiz "Ergebnis: " # comment :) restz: .asciiz "Rest : " # */ blankz: .asciiz "\n" # escaped newline character ################################################################################ .text # the text segment to store instructions .globl main # define main to be a global label ################################################################################ main: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $t0, 0 # flush $t0, used to store dividend (remainder) li $t1, 0 # flush $t1, used to store divisor li $t2, 0 # flush $t2, used to store quotient (result) li $t3, 0 # flush $t3, used as a counter variable jal _divid # call _divid to read dividend value ($t0) jal _divis # call _divis to read divisor value ($t1) jal _div # call _div to calculate the result # and remainder of the unsigned division jal _ergeb # call _ergeb to print quotient value ($t2) jal _blank # call _blank to insert a newline character jal _rest # call _rest to print remainder value ($t0) lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _div: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack _sll: sll $t1, $t1, 1 # shift divisor left one bit add $t3, $t3, 1 # increment counter variable ble $t1, $t0, _sll # repeat until divisor is aligned with dividend _sub: sra $t1, $t1, 1 # shift divisor right one bit blez $t3, _jr # stop if counter variable <= 0 sll $t2, $t2, 1 # shift quotient left one bit sub $t3, $t3, 1 # decrement counter variable bgt $t1, $t0, _sub # jump back if divisor > remainder sub $t0, $t0, $t1 # subtract divisor from remainder add $t2, $t2, 1 # increment quotient value bgtz $t3, _sub # repeat while counter variable > 0 _jr: lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _divid: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $v0, 4 # system code to print string la $a0, dividz # "Dividend: " syscall # print it! li $v0, 5 # system code to read integer syscall # read it! move $t0, $v0 # move the user's input to $t0 (dividend) lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _divis: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $v0, 4 # system code to print string la $a0, divisz # "Divisor : " syscall # print it! li $v0, 5 # system code to read integer syscall # read it! move $t1, $v0 # move the user's input to $t1 (divisor) lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _ergeb: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $v0, 4 # system code to print string la $a0, ergebz # "Ergebnis: " syscall # print it! li $v0, 1 # system code to print integer move $a0, $t2 # the argument will be the quotient's value syscall # print it! lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _blank: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $v0, 4 # system code to print string la $a0, blankz # "\n" syscall # print it! lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ _rest: sub $sp, $sp, 4 # subtract stack pointer by 4 sw $ra, 0($sp) # store return address on stack li $v0, 4 # system code to print string la $a0, restz # "Rest : " syscall # print it! li $v0, 1 # system code to print integer move $a0, $t0 # the argument will be the remainder's value syscall # print it! lw $ra, 0($sp) # load return address from stack add $sp, $sp, 4 # restore stack pointer by addition jr $ra # jump! ################################################################################ # EOF