This is sample MIPS assembler code demonstrating multiplication, power of two and addition by EzMIPS, the MIPS assembler simulator. The code is by no means optimized, but written for beginners.
Launch EzMIPS, copy the following, fully commented, MIPS code and paste it into EzMIPS. Assemble, Run.
# ------- MIPS multiplication, power of two, addition ------- #
.data
szPrompt: .asciiz "Enter value of x : "
szResult: .asciiz "2 * (x^2 + x*3) = "
# ----------------------------------------------------------- #
.text
main:
# .............. print string "Enter value of x : " ......... #
li $v0, 4 # print string
la $a0, szPrompt # load address off string to be printed
syscall # print it!
# ..................... read integer from user .............. #
li $v0, 5 # read integer
syscall
move $s0, $v0 # integer read is in $v0
# save the integer in $s0 for later use
# ......... Calculate x^2 + x*3 and store it in $t0 ......... #
mul $t0, $s0, $s0 # $t0 = x^2
addi $t2, $zero, 3 # $t2 = 3
mul $t1, $s0, $t2 # $t1 = x * 3
add $t0, $t0, $t1 # $t0 = $t0 + $t1 = x^2 + x*3
# final calculation!
add $t0, $t0, $t0 # $t0 = $t0 + $t0
# $t0 = x^2 + x*3 + x^2 + x*3
# $t0 = 2 * (x^2 + x*3)
# .............. print string "2 * (x^2 + x*3) = " .......... #
li $v0, 4 # print string
la $a0, szResult # load address off string to be printed
syscall # print it!
# ................... print integer result .................. #
li $v0, 1 # print integer
move $a0, $t0 # number to print must be in $a0
syscall # print it!
# ........................................................... #
# exit program
li $v0, 10
syscall
# ----------------------------------------------------------- #
Launch EzMIPS, copy the following, fully commented, MIPS code and paste it into EzMIPS. Assemble, Run.
# ------- MIPS multiplication, power of two, addition ------- #
.data
szPrompt: .asciiz "Enter value of x : "
szResult: .asciiz "2 * (x^2 + x*3) = "
# ----------------------------------------------------------- #
.text
main:
# .............. print string "Enter value of x : " ......... #
li $v0, 4 # print string
la $a0, szPrompt # load address off string to be printed
syscall # print it!
# ..................... read integer from user .............. #
li $v0, 5 # read integer
syscall
move $s0, $v0 # integer read is in $v0
# save the integer in $s0 for later use
# ......... Calculate x^2 + x*3 and store it in $t0 ......... #
mul $t0, $s0, $s0 # $t0 = x^2
addi $t2, $zero, 3 # $t2 = 3
mul $t1, $s0, $t2 # $t1 = x * 3
add $t0, $t0, $t1 # $t0 = $t0 + $t1 = x^2 + x*3
# final calculation!
add $t0, $t0, $t0 # $t0 = $t0 + $t0
# $t0 = x^2 + x*3 + x^2 + x*3
# $t0 = 2 * (x^2 + x*3)
# .............. print string "2 * (x^2 + x*3) = " .......... #
li $v0, 4 # print string
la $a0, szResult # load address off string to be printed
syscall # print it!
# ................... print integer result .................. #
li $v0, 1 # print integer
move $a0, $t0 # number to print must be in $a0
syscall # print it!
# ........................................................... #
# exit program
li $v0, 10
syscall
# ----------------------------------------------------------- #
Please let me know of any suggestions or bugs regarding the code above.
Regards,
Antonis
No comments:
Post a Comment