This is a sample MIPS assembler code to read two 32-bit numbers from memory and calculate the absolute value of their diferrence (ie |B-A|), store the result into memory and, finally, print it. The code is fully commented.
Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.
# ------------------------------------------------------------------------- #
# Reads word A from from word variable var01
# Reads word B from from word variable var02
# Caclculate |B-A| and store the result into word variable var03
# ------------------------------------------------------------------------- #
.data
var01: .word 116 # address =0x10010000
var02: .word 17 # address =0x10010004
var03: .word 00 # address =0x10010008
var04: .asciiz "|B-A| = "
# expected output:
# |B-A| = 99
# ......................................................................... #
.text
la $t1, var01 # Load address of var01
lw $a0, 0($t1) # read A from var01 into register $a0
lw $a1, 4($t1) # read B from var02 into register $a1
# (var02 address = address of var01 + 4)
sub $t4, $a1, $a0 # $t4 = $a1 - $a0
bgez $t4, positive # if $t4 >= 0, branch to ‘sw’ instruction
sub $t4, $a0, $a1 # else, $t4 = $a0 - $a1
positive:
sw $t4, 8($t1) # store register $t4 value, |B-A|, into var03
# (var03 address = address of var01 + 8)
# ......................................................................... #
# print "|B-A| = "
la $a0, var04 # Load address of var04
li $v0, 4 # syscall code for print string
syscall # print it!
# print integer result of |B-A|
move $a0, $t4
li $v0, 1 # syscall code for print integer
syscall # print it!
# ------------------------------------------------------------------------- #
Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.
# ------------------------------------------------------------------------- #
# Reads word A from from word variable var01
# Reads word B from from word variable var02
# Caclculate |B-A| and store the result into word variable var03
# ------------------------------------------------------------------------- #
.data
var01: .word 116 # address =0x10010000
var02: .word 17 # address =0x10010004
var03: .word 00 # address =0x10010008
var04: .asciiz "|B-A| = "
# expected output:
# |B-A| = 99
# ......................................................................... #
.text
la $t1, var01 # Load address of var01
lw $a0, 0($t1) # read A from var01 into register $a0
lw $a1, 4($t1) # read B from var02 into register $a1
# (var02 address = address of var01 + 4)
sub $t4, $a1, $a0 # $t4 = $a1 - $a0
bgez $t4, positive # if $t4 >= 0, branch to ‘sw’ instruction
sub $t4, $a0, $a1 # else, $t4 = $a0 - $a1
positive:
sw $t4, 8($t1) # store register $t4 value, |B-A|, into var03
# (var03 address = address of var01 + 8)
# ......................................................................... #
# print "|B-A| = "
la $a0, var04 # Load address of var04
li $v0, 4 # syscall code for print string
syscall # print it!
# print integer result of |B-A|
move $a0, $t4
li $v0, 1 # syscall code for print integer
syscall # print it!
# ------------------------------------------------------------------------- #
Please let me know of any suggestions or bugs regarding the code above.
Regards,
Antonis
No comments:
Post a Comment