This is a sample MIPS assembler code used to get integer input, process (plain integer addition), print integer output in MIPS.
# ---------------------- .data segment ------------------------- #
.data
szEnterA: .asciiz "Enter A: "
szEnterB: .asciiz "Enter B: "
szResult: .asciiz "A + B = "
szLF: .asciiz "\n"
# --------------- .text segment (program code) ----------------- #
.text
.globl main
main:
# print "Enter A: "
li $v0, 4 # print string syscall code = 4
la $a0, szEnterA # load the address of szEnterA
syscall # print it!
# Get integer input A from user
li $v0, 5 # read int syscall code = 5
syscall
# syscall results returned in $v0
# Save integer A in $t0
move $t0, $v0
# print "Enter B: "
li $v0, 4 # print string syscall code = 4
la $a0, szEnterB # load the address of szEnterB
syscall # print it!
# Get integer input B from user
li $v0, 5 # read integer syscall code = 5
syscall
# syscall results returned in $v0
# Save integer B in $t1
move $t1, $v0
# Perform the addition: A = A + B
add $t0, $t0, $t1
# print "A + B = "
li $v0, 4 # print string syscall code = 4
la $a0, szResult # load the address of szResult
syscall # print it!
# Print integer sum
li $v0, 1 # print integer syscall code = 1
move $a0, $t0 # integer to print must be in $a0
syscall # print it!
# Print "\n"
li $v0, 4 # print string syscall code = 4
la $a0, szLF
syscall
li $v0, 17 # exit program
li $a0, -1 # termination result = -1
syscall
# -------------------------------------------------------------- #
Please let me know of any suggestions or bugs regarding the code above.
Regards,
Antonis
No comments:
Post a Comment