Problem with Assembly subtraction

Asked

Viewed 41 times

3

My teacher sent this simple code in C, to turn it into Assembly:

int num1, num2, resultado;
printf("Digite o primeiro numero: \n");
scanf("%d", &num1);
printf("Digite o segundo numero: \n");
scanf("%d", &num2);
resultado = num1 - num2;
printf("O resultado e: %d\n", resultado); 

So far I’ve done it:

    .data
str1:    .asciiz "\nnumero 1 "
str2:    .asciiz "\nnumero 2 "
str3:    .asciiz "\nResultado: "

       .text
   .globl main
   
main:

   li $v0, 4
   la $a0, str1
   syscall
  
   li $v0, 5
   la $t1, ($t2)
   syscall
  
   li $v0, 4
   la $a0, str2
   syscall
 
   li $v0, 5
   la $s1, ($s2)
   syscall

   sub $a1, $t1, $s1
  
   li $v0, 4
   la $a0, str3
   syscall
  
   li $v0, 1
   move $a0, $a1
   syscall 

But the result is wrong (e.g. 10 - 5 = 0)?

  • Hello. Look, just to give you a feedback, the question is not poorly asked, and in my view shows some effort. But it’s very specific to your problem, so I don’t think it’s serving to help other people, which is the purpose of the site. I will vote to close but we await improvement of it to reopen and answer a more "general" question. With this even the title of the question can be improved, so that it serves better in searches made by other users.

  • 1

    I disagree with your argument. My doubt may even be the doubt other people, who are having the same problem, since it is a simple operation of subtraction.

  • I may have made a mistake. I know almost nothing about Assembly, so I made a half deduction on the basis of the assumption. It also seems that no one else has agreed with me so far. I will keep an eye and if so until I withdraw the vote and leave my +1 to the question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.