Assembly GUI programming for Mac OS X
Asked Answered
C

2

14

I’d like to know how can I do a simple assembly program for Mac OS X that shows a window on the screen and put some coloured text on that window. The code may call some Carbon or Cocoa APIs. I need some code for the nasm sintaxe.

I saw in http://snipplr.com/view/29150/assembly-code-nasm-for-mac--hello-world the next code that works fine, but it´s not graphic.

  ; Hello World in assembly for mac 
  ; 
  ; nasm -f macho hello.asm 
  ; ld -e _start -o hello hello.o 


  section .text 
  global _start ;must be declared for linker (ld) 

  _syscall: 
  int 0x80      ;system call 
  ret 

  _start:       ;tell linker entry point 

  push dword len    ;message length 
  push dword msg    ;message to write 
  push dword 1      ;file descriptor (stdout) 
  mov eax,0x4       ;system call number (sys_write) 
  call _syscall     ;call kernel 

  add esp,12        ;clean stack (3 arguments * 4) 

  push dword 0      ;exit code 
  mov eax,0x1       ;system call number (sys_exit) 
  call _syscall     ;call kernel 

  ;we do not return from sys_exit, 
  ;there's no need to clean stack 
  section .data 

  msg db "Hello, world!",0xa    ;our dear string 
  len equ $ - msg               ;length of our dear string 

Thanks for any help

Crusado answered 26/3, 2011 at 22:48 Comment(3)
+1 for hardcore assembly GUI programming in Mac OS X. Good luck.Pauperism
Also, if you want to use Cocoa, you might want to take a look at the Objective-C Runtime Reference.Pauperism
I would highly advise against trying to use objective-c directly from assembly. Just getting classes and creating instances is hard, without trying to create your own class.Barley
U
2

This is not Carbon as requested in the comments in the previous answers, but it may help you get a step further ahead in your noble pursuit:

http://cocoawithlove.com/2010/09/minimalist-cocoa-programming.html

Uzzi answered 24/7, 2011 at 20:3 Comment(0)
P
0

You can call Carbon APIs with call like this:

call _CreateNewWindow

You can pass arguments also, but I'm unsure how to do that. Probably pushed onto the stack in reversed order just before the call:

push arg4
push arg3
push arg2
push arg1
call _CreateNewWindow

You can look in how you C code compiles into assembly, like this:

$ clang myCarbonCode.c -S -O -o myCarbonCode.s
Pauperism answered 26/3, 2011 at 23:2 Comment(3)
Thank you, but could you show me a complete example and the command for compiling it with nasm and link with ld, please. Because I don’t know how can I include a file (and witch file) that instructs nasm and ld where to inspect for the api’s routines. I can do that under Linux (with JWASM) and Windows (with MASM32), but I’m getting started with Mac OS X.Crusado
You should link with the Carbon framework. I don't know Carbon and I've never used it, but the Carbon framework's binary is at /System/Library/Frameworks/Carbon.framework/Contents/MacOS/Carbon. I haven't ever used nasm or ld either. Just GCC and Clang.Pauperism
OK, could you show me a complete example in C that shows a simple window with a simple colored text on it, that could be compiled with GCC and Clang.Crusado

© 2022 - 2024 — McMap. All rights reserved.