在终端mac上运行程序集

时间:2017-11-01 14:16:45

标签: assembly terminal

这段代码应该适用于linux,有没有办法让它在终端上运行而不修改它? 这是一个课程作业,大多数学生使用Linux

这是一个简单的" Hello World!"程序

.section    .rodata #read only data section
str:    .string "Hello World!\n"
########
.text   #the beginnig of the code
.globl  main    #the label "main" is used to state the initial point of 
this program
.type   main, @function # the label "main" representing the beginning 
of a function
main:   # the main function:
pushq   %rbp        #save the old frame pointer
movq    %rsp,   %rbp    #create the new frame pointer

movq    $str,%rdi   #the string is the only paramter passed to the 
printf function (remember- first parameter goes in %rdi).
movq    $0,%rax
call    printf      #calling to printf AFTER we passed its parameters.

#return from printf:
movq    $0, %rax    #return value is zero (just like in c - we tell the 
OS that this program finished seccessfully)
movq    %rbp, %rsp  #restore the old stack pointer - release all used 
memory.
popq    %rbp        #restore old frame pointer (the caller function 
frame)
ret         #return to caller function (OS)

0 个答案:

没有答案