4
\$\begingroup\$

I am new to assembly and have made a simple addition program to sum two integers read from the keyboard. The program outputs correctly, but I want to know if there is a way to streamline my code. It seems a bit cumbersome for such a simple program and I may have instructions that are unnecessary.

# Author: Evan Bechtol
# Description: This program prompts the user to enter 2 integers and computes their sum.
#---------------------------------------------------------------------------------------#
        .data
A:          .word       # Store the number 4 as an integer in var1  # $t0 is used
B:          .word       # Store the number 2 as an integer in var2  # $t1 is used
S:          .word       # Store the sum of A and B          # $t2 is used
Prompt1:    .asciiz "Please enter first number: "
Prompt2:    .asciiz "Please enter second number: "
Result:     .asciiz "The sum of A and B is: "
        .text
main:
    #--------------------------------------------------------#
    #Display first prompt
    li  $v0, 4      # Load instruction "print string"
    la  $a0, Prompt1    # Load prompt into $a0
    syscall

    #Read first integer
    li  $v0, 5      # Read 1st integer
    la  $t0, A      # $t0 = A
    syscall

    #Store first integer into memory
    move    $t0, $v0    # Move contents in $v0 to $t0
    sw  $t0, A      # A = value at $t0 
    #--------------------------------------------------------#

    #Display second prompt
    li  $v0, 4      # Load instruction "print string"
    la  $a0, Prompt2    # Load prompt into $a0
    syscall

    #Read second integer
    li  $v0, 5      # Read 1st integer
    la  $t1, B      # $t0 = A
    syscall

    #Store second integer into memory
    move    $t1, $v0    # Move contents in $v0 to $t0
    sw  $t1, B      # A = value at $t0 
    #--------------------------------------------------------#

    #Add the two variables
    la  $t2, S      # $t2 = S   
    add     $t2, $t0, $t1   # $t2 = $t0 + $t1
    sw  $t2, S      # S = value at $t2

    #Display the Result prompt
    la  $a0, Result # Loads Output label to be printed
    li  $v0, 4      # Sysycall to print string
    syscall

    #Display the sum
    lw  $a0, S      # $a0 = value at S
    li  $v0, 1      # Syscall to print integer
    syscall

    #Exit the program
    li  $v0, 10     # Load exit code to $v0
    syscall
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$
  • The comments are misleading:

    #Read second integer
    li  $v0, 5      # Read 1st integer
    la  $t1, B      # $t0 = A
    

    umm... are we reading second or 1st? Bottomline is, do not overcomment the code.

  • syscall 5 leaves a value in $v0. The contents of $t0 (or $t1) is irrelevant during the syscall. Set them up when you need them:

    li $v0, 5
    syscall
    la $t0, A
    move    $t0, $v0
    
  • You store data to memory just to load them back. This is very anti-assembly. Generally you want to use registers as much as possible, and avoid memory as much as possible:

    li $v0, 5
    syscall
    move $t0, $v0
    ...
    li $v0, 5
    syscall
    # At this moment you have first integer in $t0, and the second in $v0.
    # Just add them together. No memory access is necessary.
    

    Consult your documentation on which registers are guaranteed to survive a syscall (I suspect, all of them besides $v0).

  • Nothing to simplify reading and printing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.