#Writing assembly in C (simple example)

22 messages · Page 1 of 1 (latest)

unique yachtBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sturdy quiver
#

What do I need to change here?

rapid hornet
#

Didn't you have it working in #c-help-text minutes ago?

dawn bridge
#

make system_time a uint64_t

#

you can't move a 64-bit register into a 32-bit variable

sturdy quiver
#

How do I use a 32-bit register instead?

dawn bridge
#

if you expect the return value to be 32-bits then use eax, not rax

sturdy quiver
#

Ah!

unique yachtBOT
#

@sturdy quiver

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

sturdy quiver
#

;compile

#include <stdio.h>
#include <time.h>
#include <stdint.h>
typedef uint32_t u32;

u32 get_system_time() {
    u32 system_time;
    __asm__ 
    (
        "mov $201, %%eax;"
        "xor %%rdi, %%rdi;"
        "syscall;"
        "mov %%eax, %0"
        : "=r"(system_time)
        :
        : "%eax", "%rdi"
    );
    return system_time;
}

int main() {
    u32 system_time = get_system_time();
    printf("Current time ASM: %llu\n", system_time);
    printf("Current time TIME.H: %u\n", (unsigned)time(NULL));
    return 0;
}
wanton pondBOT
#
Program Output
Current time ASM: 1720093164
Current time TIME.H: 1720093164
sturdy quiver
#

There we go.

main blaze
#

so you are potentially truncating the value

sturdy quiver
#

In fact that is why we have the "Year 2038" problem

rapid hornet
main blaze
#

;compile ```c
#include <time.h>
#include <stdio.h>
printf("%zu", sizeof(time_t));

wanton pondBOT
#
Program Output
8
main blaze
#

time_t is going to be 64 bits on most systems

#

including all 64 bit systems