简体   繁体   中英

How to add negative numbers in mips assembly language?

I need to sum an array of negative and positive numbers and I need to store the values in a negative sum and a positive sum. How exactly would I add negative numbers or put them in an array? Can it be done like normal numbers? Not looking for any code just a hint.

I have this code which you can use to add negative and positive values in an array. The sum is stored in register t3 and it is a negative value. I have used a loop to add the values together.

.data
array:  .word 1, 3, 5, 7, 9, -11, -13, -15, -17, 19

length: .word 10

sum:    .word 0

average:.word 0

.text

.globl main

.ent main

main:

la $t0, array #array starting index

li $t1,0 #loop index i=0

lw $t2,length #length of array

li $t3,0 #initializing sum = 0



sumloop:

lw $t4, ($t0) #get array[i]

add $t3,$t3,$t4 #addition sum = sum + array[i]



add $t1,$t1,1 #iteration

add $t0,$t0,4 #update array address

blt $t1,$t2, sumloop #loop stopping condition

sw $t3,sum #store sum in t3


li $v0,10

syscall

.end main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM