Objective C模式对话框没有弹出,程序刚刚结束

时间:2013-09-29 12:52:59

标签: objective-c c assembly nasm ld

我有以下NASM汇编代码

global _main

extern _CFStringCreateWithCString
extern _objc_msgSend
extern _objc_getClass
extern _sel_registerName


; objc macros

; get class
%macro getClass 1
    jmp %%endstr
    %%str: db %1
    %%endstr:
    sub esp, 8
    push %%str
    call _objc_getClass
    add esp, 12
%endmacro

; selector
%macro sel 1
    sub esp, 8
    jmp %%enddefsel
    %%sel_str: db %1
    %%enddefsel:
    push %%sel_str
    call _sel_registerName
    add esp, 12
%endmacro

; objc call
; selector_name, class
%macro objccall 2
    ; get selector from name
    sel %2

    ; align stack
    sub esp, 4

    ; push selector
    push eax

    ; push class
    push %1

    ; call
    call _objc_msgSend

    ; align stack
    add esp, 12
%endmacro

; create a CFSTR from a C string
%macro CFSTR 1 ; macro with one argument
    push 0
    push %1
    push 0
    call _CFStringCreateWithCString

    add esp, 12
%endmacro


; utility macros

; sz - defines a zero terminated string
%macro sz 2+
    jmp %%endstr
    %1: db %2
    %%endstr:
%endmacro

%macro store 2
    mov [%1], %2

%endmacro



SECTION .data

alert_text : db "NSAlert", 0
alloc_sel  : db "alloc", 0
init_sel   : dw "init", 0

SECTION .bss

alert_class: resb 4
popup_text : resb 4

SECTION .text
_main:

; get the class
getClass "NSAlert"

store alert_class, eax

; allocate it
objccall dword [alert_class], "alloc"

; update alert_class
store alert_class, eax

; init it
objccall dword [alert_class], "init"

; set style
; keep stack aligned note that objccall keeps it aligned internally
sub esp, 12
push dword 0001
objccall dword [alert_class], "setAlertStyle:"
add esp, 16

; set message text

; create CFSTR
sz MSGTXT, "Hello World!",0
CFSTR MSGTXT
store popup_text, eax

; keep stack aligned note that objccall keeps it aligned internally
sub esp, 12
push dword [popup_text]
objccall dword [alert_class], "setMessageText:"
add esp, 16

; set title text
; we are using the same as the message text

; keep stack aligned note that objccall keeps it aligned internally
sub esp, 12
push dword [popup_text]
objccall dword [alert_class], "setInformativeText:"
add esp, 16

; show it
; NOTE: runModal has no :
objccall dword [alert_class], "runModal"

ret

它应该等同于

#import <CoreFoundation/CoreFoundation.h>
#import <objc/runtime.h>
#import <objc/message.h>

int main(int argc, char *argv[])
{

    id  alert_class    = objc_getClass("NSAlert");

    SEL alloc_selector = sel_registerName("alloc");

    id alert_alloc = objc_msgSend(alert_class, alloc_selector);

    SEL init_selector  = sel_registerName("init");

    id alert = objc_msgSend(alert_alloc, init_selector);

    SEL alert_style = sel_registerName("setAlertStyle:");
    SEL message_txt = sel_registerName("setMessageText:");

    objc_msgSend(alert, alert_style, 1);
    objc_msgSend(alert, message_txt, CFSTR("Hello World!"));
    objc_msgSend(alert, sel_registerName("setInformativeText:"), CFSTR("Hello World!"));
    objc_msgSend(alert, sel_registerName("runModal"));
}

然后我用以下命令编译它

$ nasm -fmacho UI.asm -o UI.o
$ ld UI.o -macosx_version_min 10.8 -lSystem -framework CoreFoundation -framework AppKit -lobjc -o UI

它运行时没有错误但从不显示对话框。我相信它与我如何链接或包含objc运行时(objc / runtime.h和objc / message.h)有关。但是我不确定我是否正确链接它。

0 个答案:

没有答案