Tuesday, June 12, 2012

Assembly language on the Mac

Thanks go to Daniel Bond who allowed me to test ebe on his latest Mac laptop.  The Mac turns out to almost be great for running ebe.

At first the program was dying due to yasm not being in the path.  After solving that minor issue it worked fairly well with C and C++.  But assembly language was not quite ready for prime time.

The first issue is that the Mac standard requires prefixing global symbols with an underscore, so I had to change my test program to define _main and call _write and _exit.  I have run into this pattern before and it seems to be fairly common.  That was quickly solved and then it was time to move on.

I ran gcc with the -S option to dump assembly code and found that it thought that the segments might need to be named __text and __data, but that turned out to be a non-issue.  After a bit of effort I returned to .text and .data and managed to get my test program to link.

I ran into an issue with the command "mov  rsi, msg" where msg was a label for an array of chars to send to _write.  yasm complained that it couldn't use a 32 bit constant here.  I don't fully understand why this message occurred on the Mac and not on Linux.  However I got past this issue and used "mov  rsi, qword msg" which resulted in a program which printed "Hello World!".

Update(Nov 13, 2012) The Mac uses addresses which are too big to fit in 32 bits.  The instruction set has a 32 bit address/offset field.  Adding "default rel" tells the assembler to generate rip relative addresses.  So the better choice would be "lea  rsi, [msg]".  This is a load effective address instruction.  The address field will be the difference between the address of msg and register rip (after this instruction).  I eventually rolled my own in ebe with respect to gdb not knowing about line numbers.

That was not too difficult to cope with, but I ran into a problem for which I did see a solution.  I did not find a debugging format choice for yasm on the Mac.  Without debugging information I could build a program and execute it with no feedback.  This is not acceptable.  I poked around with nasm for a few minutes and it appears that nasm is 32 bit only.  Both the lack of a debug format and lack of 64 bit nasm seem to be problems which should have a solution.

I am on a fairly tight schedule to try to get a new edition of my assembly book done early in July.  I decided that I should not spend further time right now trying to get ebe functional with assembly language on the Mac.  I would like to explore it further later.

No comments: