I'm currently trying to run some assembly in macOS 10.13.6.
What I'm trying to achieve is get two inputs and subtracting the first one from the second one and print the result.
So... I'm trying to do something like: print -input() + input()
in python. I'm relying the input and printing to the libc, by linking with another C object file.
What I cannot understand is that when I change the stack reserving size from 8 to 4, the program crashes.
Below is the working assembly code:
.section __TEXT,__text
.globl _main
_main:
push %ebp
mov %esp, %ebp
subl $8, %esp
call _input
neg %eax
mov %eax, -4(%ebp)
call _input
add -4(%ebp), %eax
push %eax
call _print_int_nl
add $4, %esp
mov $0, %eax
leave
ret
And below is the code that does not work.
.section __TEXT,__text
.globl _main
_main:
push %ebp
mov %esp, %ebp
subl $4, %esp
call _input
neg %eax
mov %eax, -4(%ebp)
call _input
add -4(%ebp), %eax
push %eax
call _print_int_nl
add $4, %esp
mov $0, %eax
leave
ret
As you can see, there is no modification except the stack reservation size.
This is the C file I'm linking with:
#include <stdio.h>
void print_int_nl(int x) { printf("%d\n", x); }
int input() {
printf("In input...\n");
int i;
scanf("%d", &i);
return i;
}
I'm currently compiling/assembling/linking by:
$ clang -c -arch i386 runtime.c
$ as -arch i386 example.s -o example.c
$ ld example.o runtime.o -lc -arch i386
$ ./a.out