MIPS Assembly: if/else help

Started by
1 comment, last by Matei 19 years, 8 months ago
I don't see a branch for comparing two registers in my college book on MIPS. Any idea on what it would be? EDIT: I found a macro instruction ('ble') but do I need to write in something so it recognizes the instruction?

###################################################################
# Program Name: 	MIPS Assembly Language Practice
# Programmer:   	Phillip Vaira
# Date Last Modified: 	August 18, 2004
####################################################################
# Functional Description:
# A simple program that asks for the user's age and determines 
# whether it is under eighteen or over. Output would result.
####################################################################
# Pseudocode Description or Algorithm:
#
#	$t0;
# 	cout << "What is your age? ";
#	cin >> $t0;
#	if ($t0 > 17)
#	{
#		cout << "You are either eighteen or older.";
#	}
#	else
#	{
#		cout << "Your age is less than eighteen.";
#	}
#
###################################################################
# Cross References:
# $t0 = Age
###################################################################
	 .data
Prompt:  .asciiz	"\n What is your age? "
Over18:  .asciiz 	"\n You are either eighteen or older."
Under18: .asciiz 	"\n Your age is less than eighteen."
	 .globl main
	 .text

main:
	li 	$v0, 4		# system call code for Print String
	la 	$a0, Prompt	# load address of prompt into $a0
	syscall			# print the prompt message
	li	$v0, 5		# system call code for Read Integer
	syscall			# reads the value of Age into $v0
	

	li 	$v0, 10		# terminate program run and
	syscall 		# return control to system

[Edited by - The Plan 9 Hacker on August 18, 2004 10:21:43 PM]
Advertisement
To compare to registers in mips you would use the following:

beq rs, rt, offset
if (rs == rt)
PC += offset

bne rs, rt, offset
if (rs != rt)
PC += offset

"Prince, what you are you are by accident of birth; what Iam, I am through my own efforts. There have been thousands ofprinces and will be thousands more; there is only one Beethoven!"
To compare two numbers, use "slt", which means "set on less than". slt rd, rs, rt sets rd to 1 if rs<rt and to 0 otherwise. Then use bne rd, $0, whatever. Alternatively, you can subtract the two registers and use bgtz or bltz on the result.

This topic is closed to new replies.

Advertisement