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
	

Leave a Reply

Your email address will not be published.