Which compiler will I use to write a machine language?

Just out of interest, I would like to write a small program in machine code.

I am currently studying registers, ALUs, buses, and memory, and I'm a bit passionate that instructions can be written in binary instead of assembly language.

Will the compiler be used?

Preferably one that runs on OSX.

+3
source share
5 answers

You would not use a compiler to write source machine code. You would use a hex editor. Unfortunately, I am not using OSX, so I cannot provide you with a specific link to it.

, , , . ; , - .

+5

. . - 1:1 . , , http://ref.x86asm.net/ , x86. , yasm .

- , , , ,

+3

, , . , . , OSX. Linux, Windows, . , , , , , . , , , : D.

. . - , Gitmo.

.

,

+1

... ...

0

, , , , , , .

, . , , db 0xE8; dd target - ($ + 4) x86 jmp rel32. RIP- .


, add eax, ecx 01 c8 (x86). NASM db 0x01, 0xc8 ( BITS 32 BITS 64) GAS .byte 0x01, 0xc8.

, , 2 . : - . asm - , . x86 , , , add r/m32, r32 add r32, r/m32 .

MacOS, NASM - . MachO64 . AFAIK , GNU ( OS X, clang).

OTOH, NASM , , , objcopy ld.

int add(int a, int b) { return a+b; } int add(int a, int b) { return a+b; } asm x86-64 MacOS, . (MacOS C )

;section .text        ; already the default if you haven't use section .data or anything

; NASM syntax:
global _add                    ; externally visible symbol name for linking
_add:
    lea   eax, [rdi+rsi]
    ret

nasm -fmacho64 mac-add.asm 238- mac-add.o , db/. , , .

( , x86, , , ModRM + , , , , ; , . , , .)

, rbp SIB? Intel Opcode, . , , , x86, .

$ objdump -d -Mintel mac-add.o
  (doesn't support MachO64 object files on my Linux desktop)
$ llvm-objdump -d -x86-asm-syntax=intel mac-add.o

mac-add.o:      file format Mach-O 64-bit x86-64

Disassembly of section __TEXT,__text:
_add:
       0:       8d 04 37        lea     eax, [rdi + rsi]
       3:       c3      ret

, NASM, mac-raw-add.asm:

global _add
_add:                     ; we're still letting the assembler make object-file metadata
  db 0x8d, 0x04, 0x37     ; lea eax, [rdi+rsi]
  db 0xc3                 ; ret

nasm -fmacho64 . cmp mac-*.o true. C clang -O2 -g main.c mac-raw-add.o.


, , asm, - , , , 4 1- cmp eax, imm32 cmp eax, imm32 2- jmp rel8. " " ( , ).

, , . AMD L1i. , / Intel . , UOP; Agner Fog Sandybridge: " μop, .", IDK, .

, , :

global _copy_nonzero_ints
_copy_nonzero_ints:      ;; void f(int *dst, int *src)

   xor  edx, edx
   db 0x3d       ; opcode for cmp eax, imm32.  Consumes the next 4 bytes as its immediate
   ;;   BAD FOR PERFORMANCE, DON'T DO THIS NORMALLY
.loop:                        ; do {
    mov  [rdi + rdx*4 - 4], eax    ; 4 bytes long: opcode + ModRM + SIB + disp8.  Skipped on first loop iteration: decoded as the immediate for cmp
    mov  eax, [rsi + rdx*4]
    inc  edx                       ; only works for array sizes < 4 * 4GB
    test eax, eax
    jnz  .loop                ; }while(src[i] != 0)

    ret

, - , , dword . 0 dword. jmp load + , , , , . ( "do... while" ( )?)

   0:   31 d2                   xor    edx,edx
   2:   3d 89 44 97 fc          cmp    eax,0xfc974489
   7:   8b 04 96                mov    eax,DWORD PTR [rsi+rdx*4]
   a:   ff c2                   inc    edx
   c:   85 c0                   test   eax,eax
   e:   75 f3                   jne    3 <_copy_nonzero_ints+0x3>

(from yasm -felf64 foo.asm  && objdump -drwC -Mintel foo.o
 YASM doesn't create visible symbol-table entries for .label local labels
 NASM does even if you don't specify extra debug info)

, jnz , :

0000000000000000 <_copy_nonzero_ints>:
   0:   31 d2                   xor    edx,edx
   2:   3d                      .byte 0x3d

0000000000000003 <_copy_nonzero_ints.loop>:
   3:   89 44 97 fc             mov    DWORD PTR [rdi+rdx*4-0x4],eax
   7:   8b 04 96                mov    eax,DWORD PTR [rsi+rdx*4]
   a:   ff c2                   inc    edx
   c:   85 c0                   test   eax,eax
   e:   75 f3                   jne    3 <_copy_nonzero_ints.loop>
  10:   c3                      ret    

, db 0xb9, 0x7b: 2 mov ecx, 123 3 . CL , ECX 3 . , .


, . ; , , lodsd stosd stosd .

, SSE2 + 4 , . , . ( . x86/x64)

, src dst, sub rsi, rdi , add rdi, 4 , mov [rdi-4], eax store ( 7 Intel, ), mov eax, [rsi+rdi] .

0

All Articles