ID:48994
 
A rather poorly commented MIPS assembly program which asks for 2 numbers, then outputs their product. It only works with positive integers (and probably crashes with anything else) but it's my first decent assembly program and I've managed to pick up enough to produce it in about 90 minutes of learning. Yes, I'm aware that there is a built in command for multiplication, but I wanted to try out a loop.

    .data
firstnum: .asciiz "Please enter the first number\n"
seconnum: .asciiz "Please enter the second number\n"
answer : .asciiz "The answer is "
newline : .asciiz "\n"


.text
.align 2
.globl main

main:

la $v1, firstnum
jal print
jal get

move $t0, $v0

la $v1, seconnum
jal print
jal get

move $t1, $v0

li $t2, 0 # output starts at 0

bne $t1, $zero, loop # if t1 is not 0, enter the loop
j end # otherwise, end


print:
li $v0, 4
li $a0, 0
add $a0, $zero, $v1

syscall
jr $31

printnum:
li $v0, 1
li $a0, 0
add $a0, $zero, $v1

syscall
jr $31

get:
li $v0, 5
syscall
jr $31


loop:
add $t2, $t2, $t0 # add the first operand to the result
sub $t1, $t1, 1 # subtract one from the second operand

bne $t1, $zero, loop # if the second operand is > 0, continue
# otherwise, end
end:
la $v1, answer
jal print
move $v1, $t2
jal printnum
la $v1, newline
jal print

li $v0, 10
syscall


P.S. anyone think my blog font is too small?
I did about an hour of assembly before my ears started to bleed.
It's pretty easy once you have a clear idea of how you need to outline your program. I've yet to do any more advanced stuff than this, but I've been thinking about nested function calls and stacks and stuff and how you would implement them.