These small pieces of code demonstrate the differences between high and low level programming languages.


######################################################
####      Origional C++ Code
######################################################

# #include <iostream>
# using namespace std;
#
# int main() {
#    int lo,hi;
#    cout << "Enter lower bound ascii value: ";
#    cin >> lo;
#    cout << "Enter upper bound ascii value: ";
#    cin >> hi;
#    for (int i = lo; i <= hi; i++) {
#      cout << char(i) << endl;
#    }
#    return (0);
# }

######################################################
#####       Begin MIPS Assembly
######################################################

 .data

LoPrompt: .asciiz "Enter lower ascii value: "
HiPrompt: .asciiz "Enter upper ascii value: "
nl:    .asciiz "n"

 .text
 .globl main

main:   # adjust the stack
 addiu $sp, $sp, -12
 sw $ra, 8($sp)        #push ra
 sw $s0, 4($sp)        #push lo
 sw $s1, 0($sp)        #push hi

 # ask user for lo bound
 li $v0, 4            #print_string
 la $a0, LoPrompt    #a0 = "Enter lower ascii value: /n   "
 syscall

 # read in the lo bound
 li $v0, 5            #read_int
 syscall
 move $s0, $v0        #s0 = lo

 # ask user for hi bound
 li $v0, 4            #print_string
 la $a0, HiPrompt    #a0 = "Enter upper ascii value"
 syscall

 # read in the hi bound
 li $v0, 5            #read_int
 syscall
 move $s1, $v0        #s1 = hi

#### for loop ####
 add $t0, $s0 , $zero    #to = lo
for: bgt $t0, $s1, exitfor  # for (int i = lo; i <= hi; i++) {
 li $v0, 11            #print_char
 add $a0, $t0, $zero # show the current char
 syscall

 #print newline
 li $v0, 4            #print_string
 la $a0, nl             #a0 = "n"
 syscall

 # increment $t0
 addi $t0 $t0, 1        # t0 += 1

 j for


exitfor:                 #re-adjust stack
 lw $s1, 0($sp)        #pop lower
 lw $s0, 4($sp)        #pop upper
 lw $ra, 8($sp)        #pop ra

 li    $v0, 10        # system call code for exit = 10
 syscall            # call operating sys
 # END OF PROGRAM

 

Leave a Reply

Your email address will not be published.