开发者

Show negative number as substraction result in MIPS

I'm coding a small program in MIPS assambler. Given my age the program do the 4 mathematical operations (addition, substraction...).

When it substract my age (29), the result should be -7 (2 - 9 = -7), but it shows 7.

How I make MIPS to show the negative number in substraction?

Thanks in advance.

    .globl main

    .data
mensaje:    .asciiz "Hola, mi nombre es Angel Pérez.\nHasta hace poco mi edad era 29 años.\n¿Que pasa si aplico las 4 operaciones matematicas a esos dos numeros...?\n\n"
suma:       .asciiz "2 mas 9 es igual a: "
resta:      .asciiz "2 menos 9 es igual a: "
multiplicacion: .asciiz "2 por 9 es igual a: "
division:   .asciiz "2 entre 9 es igual a: "
nueva_linea:    .asciiz "\n"
.text

main:
     la $a0, mensaje
     li $v0, 4
     syscall

     la $a0, suma
     li $v0, 4
     syscall

     li $t1, 2
     li $t2, 9

     li $v0, 1
     add开发者_运维问答 $t0,$t2,$t1
     move $a0,$t0
     syscall

     la $a0, nueva_linea
     li $v0, 4
     syscall

     la $a0, resta
     li $v0, 4
     syscall

     li $v0, 1
     sub $t0,$t2,$t1
     move $a0,$t0
     syscall

     la $a0, nueva_linea
     li $v0, 4
     syscall

     la $a0, multiplicacion
     li $v0, 4
     syscall

     li $v0, 1
     mul $t0,$t2,$t1
     move $a0,$t0
     syscall

     li $v0, 10
     syscall


The subtract order is wrong:

sub $t0,$t2,$t1

You are subtracting 9-2

sub $t0,$t1,$t2

is what you need.


What did you expect 9-2 to return? The two operands of sub are inverted. The other operations also have inverted operands, but they are commutative.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜