我编译了一个代码。
.data
ssttrr:
.string "%d\n"
.text
.globl main
main:
mov $213, %rdx
push %rdx
push $ssttrr
call _printf
add $8, %rsp
或者它
.global main
.text
main:
push %rax # caller-save register
push %rcx # caller-save register
mov $format, %rdi # set 1st parameter (format)
mov %rax, %rsi # set 2nd parameter (current_number)
xor %rax, %rax # because printf is varargs
call printf # printf(format, current_number)
pop %rcx # restore caller-save register
pop %rax # restore caller-save register
mov %rax, %rdx # save the current number
mov %rbx, %rax # next number is now current
add %rdx, %rbx # get the new next number
dec %ecx # count down
jnz print # if not done counting, do some more
pop %rbx # restore rbx before returning
mov $60, %rax
xor %rdi, %rdi
syscall
format:
.asciz "%20ld\n"
为什么“格式”在 .text 字段中?
gcc -nostdlib file.s -o program
为什么我会收到错误消息?
/universe/earth/asm/file.s:14:未定义对“_printf”的引用 collect2:ld 返回 1 退出状态
如何解决此错误?
如果是这样,那么所有代码都有效。为什么???
.global main
.text
main:
push %rbx # we have to save this since we use it
mov $90, %ecx # ecx will countdown to 0
xor %rax, %rax # rax will hold the current number
xor %rbx, %rbx # rbx will hold the next number
inc %rbx # rbx is originally 1
print:
# We need to call printf, but we are using eax, ebx, and ecx. printf
# may destroy eax and ecx so we will save these before the call and
# restore them afterwards.
push %rax # caller-save register
push %rcx # caller-save register
mov $format, %rdi # set 1st parameter (format)
mov %rax, %rsi # set 2nd parameter (current_number)
xor %rax, %rax # because printf is varargs
call printf # printf(format, current_number)
pop %rcx # restore caller-save register
pop %rax # restore caller-save register
mov %rax, %rdx # save the current number
mov %rbx, %rax # next number is now current
add %rdx, %rbx # get the new next number
dec %ecx # count down
jnz print # if not done counting, do some more
pop %rbx # restore rbx before returning
ret
format:
.asciz "%20ld\n"
为什么函数 print: 不在 main 字段中调用?
cpy:intel-64 操作系统:debian