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