0

I am trying to write a program that divides 100 by a positive integer provided by the user. The program sets the dividend to 100, and then asks the user to provide the divisor. The program then calls an assembly function which is supposed to determine whether the value provided by the user was positive. If it was positive then it divides 100 by the provided divisor, and returns the quotient to the main program. If the provided divisor is negative, the function returns to the main program and kicks the user out of the program.

Right now it looks like the function is interpreting every value I put in as negative and I can't find where the problem is!

EDIT : Sorry, redid and then undid some pointer stuff. Now it's just not returning the correct remainder value. Thanks for the first tip @Jester

Here's the code:

linkage.c:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "linkage.h"

int g = 1;
int x;
static int y = 100;

int main() {
    
    

    
    printf("This is a very simple program, we're going to be dividing 100 by an integer provided.\n");
    printf("Please make sure to provide a positive integer, I may be a machine but I still don't like negativity!\n");

    printf("Please input your number:\n");
    scanf("%d", &x);
    printf("%d",x);

    int z = divide(x,y);

    if (g == 0 ) {
        printf("You entered a negative number and I don't appreciate it!\n");
    } else {
        printf("The quotient of your operation is %i, and the remainder is %i.\n", z, y);
    }
    return 0;
}

assembly.s:

.intel_syntax noprefix

.global divide

    divide:        
        cmp rdi, 0
        jge positive
        jmp negative

        positive:
            mov rax, rsi
            xor rdx, rdx
            div rdi
            mov rsi, rdx 
            ret

        negative:
            mov QWORD PTR g[rip], 0
            ret

Linakge.h:

#ifndef LINKAGE_H_
#define LINAGE_H_

int divide(int x, int y);

#endif
komsire22
  • 25
  • 3
  • 3
    Might be educational to step through in a debugger. – tadman May 23 '23 at 01:22
  • 1
    Where do you set `g` to something other than zero? – Jester May 23 '23 at 01:25
  • 1
    `mov rsi, rdx` has no effect. C arguments are passed by value. You want `mov y[rip], edx`. Also note that `int` is 32 bit. As to whether using globals like that is good practice is a different matter. – Jester May 23 '23 at 01:44
  • 1
    The caller will look in EAX for the return value. Implicitly-declared functions in C are treated as returning `int`, and `int` is 32-bit. So your return value is the low 32 bits of the `div` result. Your function doesn't set `y`. (And can't because it's `static`, and you don't pass a pointer to it.) Try writing a C function in a separate file that does you want your asm to do, to see if it's even possible. Then you can look at compiler output for that C function ([How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116)) – Peter Cordes May 23 '23 at 04:59

0 Answers0