I'm trying to count, using rdtsc, how many cycles it takes write something using sys_write. I was able to test the printf and scanf functions. They worked correctly, and now I have a problem with system calls.
In my opinion, the problem is with %eax and %edx registers, because rdtsc saves the result in these registers.
write.s
.data
SYS_EXIT = 1
SYS_WRITE = 4
STDOUT = 1
EXIT_SUCCES = 0
text: .ascii "Hello from assembler\n"
textLength: .long . - text
.section .text
.globl print
.type print, @function
print:
movl $SYS_WRITE, %eax
movl $STDOUT, %ebx
movl $text, %ecx
movl textLength, %edx
int $0x80
ret
rdtsc.s
.data
.text
.globl rdtsc
rdtsc:
push %ebx
xor %eax, %eax
cpuid
rdtsc
pop %ebx
ret
main.c
#include <stdio.h>
unsigned long long rdtsc();
extern void print();
unsigned long long startTime, stopTime, workingTime;
int main (void)
{
startTime = rdtsc();
print();
stopTime = rdtsc();
workingTime = stopTime - startTime;
printf("Cycles %llu\n", workingTime);
return 0;
}
When I run the program I get a Segmentation fault (core dumped) error.