run 32 bit assembly on 64 bit processor with mac o

2019-02-20 05:16发布

问题:

I have a problem with running 32 bit assembly on my 64 bit mac running os x 10.9.5. I also have NASM 2.11.08 installed. I am currently reading Assembly Language Step by Step by Jeff Duntemann. In the book he specifies instructions for 32 bit assembly on a linux operating system. How can I run this program on my 64 bit mac os x computer.

; eatsyscall.asm

SECTION .data           ; Section containing initialised data
EatMsg: db "Eat at Joes!",10
EatLen: equ $-EatMsg    

SECTION .bss            ; Section containing uninitialized data 

SECTION .text           ; Section containing code

global  _start          ; Linker needs this to find the entry point!

_start:
    nop         ; This no-op keeps gdb happy...
    mov eax,4       ; Specify sys_write call
    mov ebx,1       ; Specify File Descriptor 1: Standard Output
    mov ecx,EatMsg      ; Pass offset of the message
    mov edx,EatLen      ; Pass the length of the message
    int 80H         ; Make kernel call

    MOV eax,1       ; Code for Exit Syscall
    mov ebx,0       ; Return a code of zero 
    int 80H         ; Make kernel call

I have tried assembling it with

nasm -f elf -g -F stabs eatsyscall.asm

I then try to link it with

ld -o eatsyscall eatsyscall.o

but I get this error

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.6
ld: warning: ignoring file eatsyscall.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): eatsyscall.o
Undefined symbols for architecture x86_64:
  "start", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64

This should run, right? I thought that Intel's 64 bit processors were capable of running 32 bit programs. Or is there no way to run an assembly program written for 32 bit linux systems on a 64 bit mac?

Do I need to install some set of 32 bit libraries to be able to link this file? Should I use something other than NASM such as GCC? Or is the program itself not written correctly. Thanks for the help!

回答1:

An executable for Linux won't run on a Mac, period. Install Linux on a virtual machine on your Mac, if you want to run Jeff Duntemann's stuff. The code can be translated to -f macho64 fairly(?) easily, but there's a bad bug in Nasm-2.11.08 on -f macho64 :(

There's a release candidate - http://www.nasm.us/pub/nasm/releasebuilds/2.11.09rc1/macosx/ - which "may" fix it. Someone needs to test it. Perhaps not a good job for a beginner. You should be able to program using gcc with your Mac, but not using "Step by Step". Nasm will work on your Mac... but not right now... Install Linux if you can, for now.



回答2:

You have two problems here.

  1. You are compiling your assembly file to an ELF binary (-f elf), which is not supported by the Mac OS X ld. Use -f macho to generate Mach-O object files for your system, then use -arch i386 to link it as a 32-bit binary.

  2. You are trying to use Linux system calls on Mac OS X. This does not work; the system call numbers and calling conventions are different, and are not publicly documented. Fixing this is possible, but, as Frank Kotler mentions, it's not a task I'd recommend for a beginner; your best bet will be to use a 32-bit Linux system to complete these tutorials.