Another look at C++ vs. Assembly

This little program is written in both C++ ans MIPS assembly code. It asks the user for their name and age, then repeats the info back to the screen. Notice the manual adjustment of the runtime stack in the assembly version.

#########################################
####   C++ CODE
#########################################

# void GetInfo(string& name, int& age);
# void DisplayInfo(string name, int age);

# int main() {
#   string name;
#   int age;

#   GetInfo(name, age);
#   DisplayInfo(name, age);

#   return (0);
# }

# void GetInfo(string& name, int& age) {
#   cout << "Enter your name: ";
#   cin >> name;
#   cout << "Enter your age: ";
#   cin >> age;
# }

# void DisplayInfo(string name, int age) {
#   cout << "So " << name
#        << " you are "<< age << " years old eh?";
# }

##########################################
####    Same Algorithm in MIPS
##########################################
	.data
nameprompt:	.asciiz "Enter your name: "
ageprompt:	.asciiz "Enter your age: "
wordso:		.asciiz "So "
wordyouare:	.asciiz " you are "
wordyearsoldeh:	.asciiz " years old eh?"
buf: .space 30	# Name Buffer

	.text
	.globl main

main:

	#addiu $sp,$sp, -4
	#sw $ra,0($sp)	# preserve $ra

	jal GetInfo

	jal DisplayInfo

	lw $ra, 0($sp)		# pop ra
	addiu $sp, $sp, 4

	li	$v0, 10		# system call code for exit = 10
	syscall				# call operating system

##########################
#####    MIPS Functions
##########################

	GetInfo:
	li $v0, 4		# print_string
	la $a0, nameprompt		# a0 = "Enter your name "
	syscall
<?php the_content('&raquo; &raquo; &raquo; &raquo;'); ?>
	# read inname string
	li $v0, 8		# read_str
	la $a0, buf
	la $a1, 30
	syscall
	move $s0, $v0		# s0 = &name

	# ask the user's age
	li $v0, 4		# print_string
	la $a0, ageprompt		# a0 = "Enter your age"
	syscall

	# read in age
	li $v0, 5		# read_int
	syscall
	move $s1, $v0		# s1 = age
	jr $ra

	DisplayInfo:

	# print "So "
	li $v0, 4		# print_string
	la $a0, wordso		# a0 = "So "
	syscall

	#print name
	li $v0, 4		# print_string
	#la $a0, 0($sp)		#
	la $a0, buf
	syscall
	move $a0, $s0

	# print " you are "
	li $v0, 4		# print_string
	la $a0, wordyouare		# a0 = "you are "
	syscall

	# print age
	li $v0, 1		# print_int
	move $a0, $s1	# a0 = age
	syscall

	# print " years old eh?"
	li $v0, 4		# print_string
	la $a0, wordyearsoldeh		# a0 = " years old eh?"
	syscall

	jr $ra

C++ and Assembly side by side

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