Display a 64-bit integer in MIPS

0

I am using MARS and MIPS for a CS assignment where I am supposed to calculate ((AB)+(CD)) given any 32-bi unsigned integers. Ultimately, I am getting the right answer which is supposed to be stored in $t8 (most significant bits) and $t9 (least significant bits). When doing something along the lines of (2 billion * 4) + (4 * 4) I should display 8,000,000,016, however as soon as I enter the 2 billion my result is displayed as -589934592 despite the correct answer being shown in $t8 and $t9 ($t8 = 0x00000001 and $t9 = 0xdcd65010)

I should note that currently, I have 3 results being displayed, first the result of AB, then CD, and finally ((AB)+(CD)). No matter when I input 2 billion all my results will display as -589934592, so if I don't input the 2 billion until C or D my result for A*B is correct but after that, I don't know what happens.

    .data
    A:          .word       # Integer A
    B:          .word       # Integer B
    C:      .word   # Integer C
    D:      .word   # Integer D
    S:      .word   # Integer S to store high 32 bits of result
    S1:     .word   # Integer S1 to store low 32 bits of result
    newline:    .asciiz "\n" #To make output look better
    Prompt1:    .asciiz "Please enter first number A: "
    Prompt2:    .asciiz "Please enter second number B: "
    Prompt3:    .asciiz "Please enter third number C: "
    Prompt4:    .asciiz "Please enter fourth number D: "
    Result:     .asciiz "The result of ((A * B) + (C * D)) : "
    Partial:    .asciiz "The result of (A * B) "
    Partial2:   .asciiz "The result of (C * D) "

    .text
    main:
#Prompt for integers A
li $v0, 4          # Load instruction for printing the string
la $a0, Prompt1    # Load Prompt1 into $a0
syscall

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

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

##Prompt for integers B
li $v0, 4          # Load instruction for printing the string
la $a0, Prompt2    # Load prompt2 into $a0
syscall

#Read second integer
li $v0, 5      # Read second integer
la $t1, B      # $t1 = B
syscall

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


#Multiply the two variables A and B and store it in S
la $t2, S      # $t2 = S
mult $t0, $t1  # A * B
mfhi $t8    # hold high 32-bits of A*B
mflo $t9    # hold low 32-bits of A*B

sw $t8, S   # S = value at $t8
sw $t9, S1  # S1 = value at $t9

#Display the Result prompt
la $a0, Partial # Loads Output label to be printed
li $v0, 4      # Sysycall to print string "The result of A*B"
syscall

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

#New line    
la $a0, newline  
addi $v0, $0, 4     
syscall

#Prompt for integers C
li $v0, 4          # Load instruction for printing the string
la $a0, Prompt3    # Load Prompt3 into $a0
syscall

#Read third integer
li $v0, 5      # Read third integer C
la $t0, C      # $t0 = C
syscall

#Store third integer C into the memory
move    $t0, $v0  # Move contents in $v0 to $t0
sw $t0, C     # C = value at $t0

##Prompt for integers D
li $v0, 4          # Load instruction for printing the string
la $a0, Prompt4    # Load prompt4 into $a0
syscall

#Read second integer
li $v0, 5      # Read fourth integerD
la $t1, D      # $t1 = D
syscall

#Store second integer into memory
move    $t1, $v0    # Move contents in $v0 to $t1
sw $t1, D      # D = value at $t1


#Multiply the two variables C and D and store it in S
la $t2, S      # $t2 = S
mult $t0, $t1  # C * D
mfhi $t2    # holds high 32-bits of C*D
mflo $t3    # holds low 32-bits of C*D

#Store new C*D values in S and S1 (A*B are in S and S1 but also in $8 and $t9)
sw $t2, S
sw $t3, S1    

#Display the Result prompt
la $a0, Partial2 # Loads Output label to be printed "Result of C*D"
li $v0, 4      # Sysycall to print string
syscall

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

#New line    
la $a0, newline  
addi $v0, $0, 4     
syscall

#Add partial results
add $t8, $t8, $t2 # Add high values of A*B = $t8 and high values of C*D = $t2 and store in $t8
add $t9, $t9, $t3 # Add low values of A*B = $t9 and low of values of C*D= $t3
sw $t8, S # load high values of addition in $t8
sw $t9, S1 # load low values of addition in $t9

#New line    
la $a0, newline  
addi $v0, $0, 4     
syscall

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

#Display results
lw $a0, S #set up to print addition results
li $v0, 1
lw $a0, S1
li $v0, 1
syscall

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

# At this point I am pulling the correct solution - and displaying correct answers when 
numbers are small
# but when numbers are bigger (such as (2b*4)+(4*4)) I am getting the right answer but not 
displaying it right
# 64-bit representation of complex example above is being seperated correctly into $t8 and 
$t9 but no showing right
# $t8 = 0x00000001 and $t9 = 0xdcd65010 so together 0x00000001dcd65010 which is 
8,000,000,016 but the all the displays
# after 2b is entered is -589934592 despite the correct answer being held in $t8 and $t9 

I am sure my code is a mess but this is my first attempt to write in MIPS, I just don't get why I am getting the right solution in $t8 and $t9 but am not displaying it correctly. I appreciate any feedback or ideas I receive and thank everyone for reading this long post.

binary
mips
asked on Stack Overflow Feb 13, 2020 by A person

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0