hybrid.s
.section .text # specifices the beginning of the 'text' or code section
.global collatz # makes the name 'collatz' visible to the linker
collatz: # int collatz(long long n) {
ret # return
hybrid.c
#include <stdio.h>
#include <stdlib.h>
extern int collatz(long long n);
int main(int argc, char *argv[]){
if (argc < 2) {
printf("Parameter \"n\" is missing. \n");
return -1;
}
int output=0;
long long n = atoll(argv[1]);
for (long long i=1 ; i<n ; i++) {
output = collatz(i);
printf("collatz(%lld) is %d\n", i,output);
}
}
I was testing what %rax
is initialized to by doing
>> gcc -o hybrid hybrid.c hybrid.s
>> ./hybrid 5
Yielding:
collatz(1) is 1
collatz(2) is 2
collatz(3) is 3
collatz(4) is 4
I expected hybrid.s to always return 0, since my guess was that %rax is initialized to 0; but as you can see, this is not the case. By observing the output, my hyptothesis is that %rax is equal to %rdi by default?
This prompts the question: What is %rax
initialized to?
EDIT:
In the following whenever I say "run hybrid.s isolated", I mean "run hybrid.s isolated with all occurrences of collatz replaced by '_start'", of course.
Based on @ErikEidt's comment, Shouldn't running hybrid.s isolated as follows result in just returning whatever %rax was before (last set to), since it wasn't initialized? Why do I then get a Segmentation fault (core dumped)
?
>> as hybrid.s -o hybrid.o
>> ld hybrid.o hybrid
>> ./hybrid 5
Yields
Segmentation fault (core dumped)
I mean the %rax has to contain some value at any given moment .. right? So why a segmentation fault instead of just returning that value?