正如@PeterCordes 提到的,您可以使用times 或clock_gettime。
但我更喜欢clock_gettime 与内核约定0x71 在寄存器x8 根据您的参考表。
这是我的代码,注意我使用的是模块化编程汇编,所以需要.include。
.data
//reseve 8 bytes for second and 8 bytes for nanosecond
TIME_OUTPUT: .zero 16
//line feed alloc for print newline
LF: .ascii "\n"
.text
.global _start
.include "exit.s" // import exit
.include "print.s" // import print library
_start:
// getting current timestamp
mov x0, 0 // x0=0 means CLOCK_REALTIME
ldr x1, =TIME_OUTPUT // store output in specified addres
mov x8, 0x71 // kernel convention
svc 0 //execute
// processing output
ldr x2, [x1] // load second
ldr x3, [x1, 8] // load nanosecond
ldr x4, =1000000000 //nanosecond unit for multiplier
mul x2, x2, x4 // multiply second
add x0, x2, x3 // add second + nanosecond store in x0
// print Unsigned Integer 64bit
bl _print_u64 // the argument is in x0 which unsigned integer in bin>
// print Line Feed (newline)
ldr x0, =LF
mov x1, 1
bl _print
_exit:
exit 0 // just exit gracefully with exit code 0
输出:
1657248826648171250
所以如果你还没有制作打印功能,你必须制作print.s。
/* Function Usage
_print <x0: start address> <x1: length>
"Print string based on address offset and length."
eg: mov x0, 0x12345; mov x1, 5; bl _print
_print_uint64 <x0: uint64 in binary form>
"Print string char format from uint64."
eg: mov x0, #12345; bl _print_uint64 // 12345
*/
/* Macro Usage
print_str <string>
"Print string to the standard output."
eg: print_str "Hello world!\n"
print_uint64 <uint64>
"Print string char format from uint64 to the stdout."
eg: print_uint64 123456
*/
// Register Dependencies: x0 until x8
.type _print, @function
.global _print
_print: //args: x0 start_address, x1 length
mov x2, x1; mov x1, x0 //moving arguments x0, x1
mov x0, 1; mov x8, 0x40; svc 0 //system call
ret //void
.type _print_u64, @function
_print_u64: //args: x0 uint64 in binary form
mov x1, 10 //divisor
mov x5, 0 //total digit
_dividing:
udiv x2, x0, x1; msub x3, x2, x1, x0 //x2 quotient, x3 remainder
add x4, x3, 0x30 //convert remainder to ascii char
sub x5, x5, 1; strb w4, [sp,x5] //store ascii char
mov x0, x2; cmp x0, 0; b.ne _dividing //jump if quotient not zero
mov x0, sp; add x0, x0, x5 //x0 args
neg x1, x5 //x1 args
mov x6, lr //backup lr
bl _print
mov lr, x6 //restore lr
ret //void
.macro print_str string
.pushsection .data
str\@: .ascii "\string"
len\@= .-str\@
.popsection
ldr x0, =str\@
mov x1, len\@
bl _print
.endm
.macro print_u64 unsigned_int64
.pushsection .data
uint64\@: .8byte \unsigned_int64
.popsection
ldr x0, uint64\@
bl _print_uint64
.endm
还有exit.s
/*
exit <code>
"Exit the program with code, 0 means success."
eg: exit 0
*/
.macro exit code
mov x0, #\code
mov x8, #0x5d
svc #0
.endm
以下是简要说明:
您只需要关注getting current timestamp 并处理输出。
您将获得返回值,该值将存储在寄存器x1 中存储的内存地址中。例如当前秒是
1657248826 在[x1]
当前纳秒是
648171250.
然后在processing output
你只需要这样
ts = 1657248826
ts = ts * 1000000000 // because nano is there are 9 zeros for nano unit
ts = ts + 648171250
/So the final is
ts = 1657248826648171250