Я пытаюсь скомпилировать код сборки в 32-разрядной версии Linux Ubuntu. Но когда я компилирую его с nasm -felf32 assembly.asm && gcc assembly.o && ./a.out
, я сталкиваюсь с Segmentation fault (core dumped)
. Я так понял, что проблема с printf.
Я много искал в Интернете, было несколько таких вопросов. Но ни один из ответов не сработал для меня.
global main
extern printf
section .text
main:
push ebx ; we have to save this since we use it
mov ecx, 90 ; ecx will countdown to 0
xor eax, eax ; rax will hold the current number
xor ebx, ebx ; rbx will hold the next number
inc ebx ; rbx is originally 1
print:
; We need to call printf, but we are using rax, rbx, and rcx. printf
; may destroy rax and rcx so we will save these before the call and
; restore them afterwards.
push eax ; caller-save register
push ecx ; caller-save register
mov edi, format ; set 1st parameter (format)
mov esi, eax ; set 2nd parameter (current_number)
xor eax, eax ; because printf is varargs
; Stack is already aligned because we pushed three 8 byte registers
call printf ; printf(format, current_number)
pop ecx ; restore caller-save register
pop eax ; restore caller-save register
mov edx, eax ; save the current number
mov eax, ebx ; next number is now current
add ebx, edx ; get the new next number
dec ecx ; count down
jnz print ; if not done counting, do some more
pop ebx ; restore rbx before returning
ret
format:
db "%20ld", 10, 0
*** исходный код скопирован с учебного сайта Nasm. Это для печати чисел Фибоначчи.
push e[?]x
илиpop e[?]x
, когда ожидает использования 64-битного набора инструкций. Так что, возможно, перепроверьте параметры и/или версию Nasm? - person aroth   schedule 18.06.2015printf
64-битным способом (mov edi, format
). В 32-битном режиме аргументы передаются в стеке, а не в регистрах. В 64-битном режиме вы не можете использовать 32-битные регистры (push eax
). Предложение: оставайтесь в 32-битном режиме, соответствующим образом измените вызов printf и скомпилируйте с помощьюgcc -m32 ...
. - person rkhb   schedule 18.06.2015