Jump to content

Assembly issue

ShatteredPsycho

Hello everyone,

 

I am doing a program in assembly (32bit Ia32) wich has to: 

" Implement a function that, given a number (a 32-bit integer value declared in C), computes its result according to the following set of successive steps:

a) Subtracts 1

b) Multiplies by 3

c) Adds 12 d) Divides by 3

e) Adds 5

f) Subtracts num The obtained result should always be 8."

 

Now, here is my code: ".s"

.section .data
	
	.global num
	.global res
	
.section .text
	.global menos 
menos:
	    movl res, %eax  #move res to eax
	    subl $1, %eax  #subtract 1 to eax
	    ret
	
	.global vezes
vezes:
	    movl res, %eax 
	    imull $3, %eax #multiply eax by 3 and save in eax
	    ret

	.global soma
soma:
	    movl res, %eax 
	    addl $12, %eax #add 12 to eax
	    ret

	.global div
div:
	    movl $3, %ebx  #change the value of ebx to 3
	    movl res, %eax
	    cdq  #extends eax to eax:edx
	    idivl %ebx  #divides eax:edx by ebx
	    ret

	.global soma2
soma2:
	    movl res, %eax 
	    addl $5, %eax  #add 5 to eax
	    ret

	.global sub2
sub2:
	    movl res, %eax 
	    subl num, %eax #subtracts num to %eax
	    ret

 

".c"

#include <stdio.h>
#include "ex14.h"

  int num, res;

int main(void) {
    
    printf("Escreva um número: ");
    scanf("%d",&num);
    res = num;
    res = menos();
  
    printf("Subtrair 1: %d\n", res);
  
    res = vezes();
    printf("Multiplicar por 3: %d\n", res);
  
    res = soma();
    printf("Adicionar 12: %d\n", res);
  
    res = div();
    printf("Dividir por 3: %d\n", res);
  
    res = soma2();
    printf("Adicionar 5: %d\n", res);
  
    res = sub2();
    printf("Subtrair o valor inicial: %d\n", res);
    
    printf("O valor final é %d\n", res);

    return 0;
}

 

When I use the number 80, for example, the result is 88, and it should be 8

 

Can anyone help?

Appreciate it

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, gabrielcarvfer said:

@ShatteredPsycho, I'm not sure of which assembly language are you using, but to me it seems that you're passing inverted parameters to assembly instructions.

 

At least in masm and nasm, the destination is given by the first parameter of instruction.

 

Other thing that can be wrong is that res is a pointer, so you should access its contents, like [res].

 

When you declare it as global, what you do is insert its label to symbol table, so that it can be linked, but you access it the same way that you did in the example I gave you the other day.

 

  Hide contents


section .data
        num dq 80
	res dq 80
	
section .text
menos:
	    mov eax, [res];  #move res to eax
	    sub eax, 1 ;#subtract 1 to eax
	    ret
	
vezes:
	    mov eax, [res] 
            mov edx, 3
	    mul edx; #multiply eax by 3 and save in eax
	    ret

soma:
	    mov eax, [res] 
	    add eax, 12 ;#add 12 to eax
	    ret

div:
	    mov ebx, 3;  #change the value of ebx to 3
	    mov eax, [res];, %eax
	    div ebx  ;#divides eax:edx by ebx
	    ret

soma2:
	    mov eax,[res] 
	    add eax,5  ;add 5 to eax
	    ret

sub2:
	    mov eax, [res] 
	    sub eax, [num] ;subtracts num to %eax
	    ret

section .text
global CMAIN
CMAIN:
    
    call menos
    mov [res], eax
    call vezes
    mov [res], eax
    call soma
    mov [res], eax
    call div
    mov [res], eax
    call soma2
    mov [res], eax
    call sub2
    mov [res], eax
     
    ret

 

 

In the language that I am using the parameter on the right "stores" the result of the instruction\operation. I'll try to know what it's name is.

 

I see that you are brazilian so i'll just write in portuguese

 

Obrigado pela ajuda, ainda tou a tentar aprender tudo isto e vai tudo um bocado ao calhas

 

Abraço!

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, gabrielcarvfer said:

Just found it, you're using GAS (GNU Assembly). 

 

To fix your code, try including parenthesis around the memory pointer, like the following:


movl (res), %eax

 

P.S.: De boas cara.

Eu recomendo usar o Nasm, bem mais amigável, sintaxe marota, dá para fazer as mesmas coisas. 

No GAS vai ter que ficar separando mnemônicos por tamanho, colocando sufixo, enquanto no NASM o tamanho do registrador já determina qual é a operação correspondente com base no mnemônico.


Deixar um link com algumas diferenças, mas vai do gosto do freguês 
¯\_(ツ)_/¯

Obrigado mas no meu caso não há muita escolha, tem de ser assim mesmo :/

 

Obrigado por toda a ajuda, entretanto se não te importares quando tiver umas dúvidas venho aqui chatear-te um pouco xp

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×