Sign in to follow this  

MIPS Assembly: if/else help

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this