0

I am new to assembly and I am trying to write a program that computes C + A. C is a 32-bit variable, while A is an 8-bit variable.

This is the code I have: (variables are global so they can be changed in main.c)

# prologue
pushl %ebp # save previous stack frame pointer
movl %esp, %ebp # the stack frame pointer for sum function

#body of the function
movl $0, %edx
movl $0, %eax
movsbl A, %eax
addl C, %eax            #eax = eax + C

#epilogue
movl %ebp, %esp # restore the previous stack pointer ("clear" the stack)
popl %ebp # restore the previous stack frame pointer
ret

For some reason, when A = 0, and C = 1, I get the value 49. If A is 1 and C is 1, I get the value 50. I have done a debug, and I can see that when it reaches the command "movsbl A, %eax" the register eax always gets the value 48 + whatever value I give A. Don't know why this is happening. In main.c the variable A is set to 0, but even if I change to, let's say 5, it still gives me 48 in eax. I've tried numerous solutions.

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

char A=0;
int C=0;

 
int main(void) {
    printf("Valor A:");
    scanf("%s",&A);
    printf("Valor C:");
    scanf("%d",&C);

    long long num = sum_and_subtract();

    printf("sum_and_subtract = %lld:0x%x\n", num, num);

    return 0;
}

Any suggestions would by highly appreciated!

0 Answers0