Time to write some MIPS assembly language code. Here are examples, to guide this effort.
Conditional execution is the equivalent of a high level language if statement. Sometimes an instruction (or a set of instructions) should be executed, and sometimes it (they) should not.
general format of Pascal if-then-else
if (condition) then
statement
else
statement;
general format of C or JAVA if-then-else
if (condition)
statement;
else
statement;
If statement example:
# MAL code fragment for the C or Java code:
#
# if (count < 0)
# count = count + 1;
#
lw $8, count
bltz $8, ifstuff
b endif
ifstuff: add $8, $8, 1
endif: sw $8, count
# next program instruction goes here
OR,
lw $8, count
bgez $8, endif
add $8, $8, 1
endif: sw $8, count
# next program instruction goes here
And, if variable count
is already in register $8,
and intended to be re-used,
then count
does not need to be loaded and stored:
bgez $8, endif
add $8, $8, 1
endif: # next program instruction goes here
Examples of compound conditional:
# MAL code fragment for the C or Java code:
#
# if ( (x < y) || (w == z) ) {
# a = a + 1;
# }
#
lw $8, x
lw $9, y
lw $10, w
lw $11, z
blt $8, $9, increment # no need to check second
bne $10, $11, no_increment # condition if first is True
increment: lw $12, a
add $12, $12, 1
sw $12, a
no_increment: # next program instruction goes here
# MAL code fragment for the C or Java code:
#
# if ( (x < y) && (w == z) ) {
# a = a + 1;
# }
#
lw $8, x
lw $9, y
lw $10, w
lw $11, z
bge $8, $9, no_increment # must check second
bne $10, $11, no_increment # condition if first is True
lw $12, a
add $12, $12, 1
sw $12, a
no_increment: # next program instruction goes here
Structured loops can be built out of IF's and GOTO's.
while loop example:
# MAL code fragment for the C or Java code:
#
# while (count > 0) {
# a = a % count;
# count--;
# }
#
# Assume that $8 has variable count
# $9 has variable a
#
while: blez $8, endwhile
rem $9, $9, count
sub $8, $8, 1
b while
endwhile: # next program instruction goes here
do while loop example:
(NOTE: This example shows an implementation of nonsense code.)
# MAL code fragment for the C or Java code:
#
# /* do statement while expression is TRUE */
# /* when expression is FALSE, exit loop */
# do {
# if (aa < bb)
# aa++;
# if (aa > bb)
# aa--;
# } while( aa != bb);
#
# Assume that $8 has variable aa
# $9 has variable bb
#
repeat: bge $8, $9, secondif
add $8, $8, 1
secondif: ble $8, $9, until
sub $8, $8, 1
until: bne $8, $9, repeat
while loop example:
# MAL code fragment for the C or Java code:
#
# while ( (count < limit) && (c==d) ) {
# /* loop's code goes here */
# {
#
# Assume that $8 has variable count
# $9 has variable limit
# $10 has variable c
# $11 has variable d
#
while: bge $8, $9, endwhile
bne $10, $11, endwhile
# loop's code goes here
b while
endwhile:
for loop example:
# MAL code fragment for the C or Java code:
#
# for ( i = 3; i <= 100; i++) {
# a = a + i;
# }
#
# Assume that $8 is the loop induction variable i
# $9 has variable a
# $10 is the constant value 100
li $10, 100 # set constant
li $8, 3 # initialize loop induction variable
for: bgt $8, $10, endfor
add $9, $9, $8
add $8, $8, 1 # increment loop induction variable
b for
endfor:
Putting this all together, we can write programs. Here is a sample MAL program.
# this simple MAL program reads in 2 characters, figures # out which one is alphabetically first, and prints it out. # register assignments # $8 -- the first character typed by the user # $9 -- the second character typed by the user # $10 -- temporary # $11 -- holds the value of the larger character # $13 -- the address of the newline character constant # $14 -- newline character (a constant) .data newline: .byte '\n' .text __start: getc $8 # get 2 characters getc $9 la $13, newline # print out newline lb $14, ($13) putc $14 sub $10, $9, $8 # figure out which is larger bgez $10, secondlarger add $11, $8, $0 b printresult secondlarger: add $11, $9, $0 printresult: putc $11 end: done
There are several things to notice about this program.
.data
section of the program.
This section typically appears first within the source code file.
.text
section.
__start
. This special label has 2 underscore characters in it.
A program example that prints the alphabet:
# MAL program to print out the alphabet .data str1: .asciiz "The alphabet:\n" # register assignments # $8 -- the ASCII character code to be printed # $9 -- the ASCII code for 'z', the ending character .text __start: la $10, str1 puts $10 add $8, $0, 97 # $8 gets ASCII code for 'a' # could be li $8, 97 add $9, $0, 122 # $9 gets ASCII code for 'z' # could be li $9, 122 while: bgt $8, $9, all_done putc $8 add $8, $8, 1 b while all_done: li $10, '\n' # print newline character putc $10 done
A program example that reads characters that form an integer, and then prints them back out:
# a MAL program to print out the ? of a user-entered integer. .data # prompts str1: .asciiz "Enter an integer: " str2: .asciiz "The result is " str_error: .asciiz "\nInput error detected. Quitting.\n" newline: .byte '\n' # variables int_array: .word 0:20 # array to hold integer for printing .text __start: la $8, str1 # print prompt puts $8 lb $10, newline # read characters and calculate li $11, 57 # the integer represented li $12, 48 getc $9 get_chars: beq $9, $10, got_int # newline char terminates loop bgt $9, $11, int_error blt $9, $12, int_error sub $13, $9, 48 # convert char to digit mul $14, $14, 10 # int = int * 10 + digit add $14, $14, $13 getc $9 b get_chars int_error: la $8, str_error puts $8 j end_program got_int: # $14 -- the integer to be printed # $15 -- base address of array holding the integer # $16 -- running address of array element # $17 -- single digit of the integer # $18 -- single character of the integer print_int: la $8, str2 puts $8 la $15, int_array move $16, $15 more_digits: rem $17, $14, 10 sw $17, ($16) add $16, $16, 4 div $14, $14, 10 bgtz $14, more_digits sub $16, $16, 4 bge $16, $15 more_chars # test for result = 0 putc '0' putc $10 # print newline more_chars: lw $18, ($16) add $18, $18, 48 putc $18 sub $16, $16, 4 bge $16, $15, more_chars end_program: putc $10 done
Assembly language source code tends to follow style conventions. Follow these:
Copyright © Karen Miller, 2006 |