2014-11-12 6 views
3

NASM과 함께 Mac에서 어셈블러 64에 간단한 helloworld를 쓰려고합니다. 내가 시도 할 때마다 실행하기 위해 나는이 오류 받고 있어요 :불법 명령어 : 4 (Mac 64 비트, NASM)

nasm -f macho64 HelloWorld.asm 

그리고 나 ':

section .text 
global _main 

_main: 
    mov rax, 4 
    mov rbx, 1 
    mov rcx, tekst 
    mov rdx, dlugosc 
    int 80h 

    mov rax, 1 
    int 80h 

section .data 

tekst db "Hello, world", 0ah 
dlugosc equ $ - tekst 

나는 컴파일 해요 : 여기

Illegal instruction: 4 

은 내 코드입니다 m 연결 :

ld -o HelloWorld -arch x86_64 -macosx_version_min 10.10 -lSystem -no_pie HelloWorld.o 

도움을 주시면 감사하겠습니다.

+0

는 디버거에서 그것을 실행 해보십시오. –

+0

'-macosx_version_min'과'10.10' 사이에'= '가 있어야합니다, 맞습니까? – harold

+0

[이 페이지] (https://filippo.io/making-system-calls-from-assembly-in-mac-os-x/)를 믿는다면 잘못된 syscall 번호를 사용하고 인수가 잘못된 레지스터에 있습니다. –

답변

2

의 가장 중요한 것은 시작하자 : 출구 것이 0x2000001 할 수 있도록

맥 OSX에

, 시스템 호출은 0x2000 인 ###로 시작된다.

다음으로 올바른 인수를 사용하여 인수를 전달해야합니다.

The number of the syscall has to be passed in register rax. 

rdi - used to pass 1st argument to functions 
rsi - used to pass 2nd argument to functions 
rdx - used to pass 3rd argument to functions 
rcx - used to pass 4th argument to functions 
r8 - used to pass 5th argument to functions 
r9 - used to pass 6th argument to functions 

A system-call is done via the syscall instruction. The kernel destroys registers rcx and r11. 

그래서 이것을 함께 가져 오는 코드의 고정 된 버전은 다음과 같습니다

section .text 
global _main 

_main: 
    mov rax, 0x2000004 
    mov rdi, 1 
    mov rsi, tekst 
    mov rdx, dlugosc 
    syscall 

    mov rax, 0x2000001 
    syscall 

section .data 

tekst db "Hello, world", 0ah 
dlugosc equ $ - tekst 
관련 문제