0

I'm taking a course on computer structure and I'm lost. I was given the following file: Hello.s:

#This is a simple "Hello World!" program
    .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)

I tried the most naive thing, went into the terminal, and wrote:

gcc Hello.s

I got the following messages:

Hello.s:2:19: error: unexpected token in '.section' directive
 .section .rodata #read only data section
                  ^
Hello.s:7:2: error: unknown directive
 .type main, @function # the label "main" representing the beginning of a function
 ^

I'm completely lost and I have no clue what I should do. My professor is of no help, and almost all the other students own a Windows pc and figured it out in different ways.

What am I suppose to do? Is there a program I should download? Anything at all I could do before going and buying a Windows computer?

Please be very specific and easy on your answer since I'm no expert under any means.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ucei
  • 51
  • 5
  • 2
    It appears the comments at the end of `.section` directive is causing a problem. Try removing the comment on that line and see what happens. I don't think the MacOS assembler supports the `.type` directive. Try removing that line altogether. – Michael Petch Oct 25 '20 at 21:21
  • Your MacBook is also x86 and can run Windows, the same as any other "PC". The actual difference is the operating system, not the hardware. – Acorn Oct 25 '20 at 21:34
  • @MichaelPetch unfortunately I can't edit the Hello.s file itself. Just need to find a way to compile and run it from my computer. – ucei Oct 25 '20 at 21:39
  • 3
    Given this line `movq $str,%rdi` you may not be able to assemble and link this for MacOS even if you had fixed the other problems. In MacOS the function would also have to be called `_main` and not `main`. If you can't modify hello.s then get VirtualBox (free) or some other emulator or VM, install Linux in it and then compile and assemble this code in the virtual machine. – Michael Petch Oct 25 '20 at 21:43
  • Yeah, you literally can't run this in a normal way on MacOS; `movq` implies a 32-bit sign-extended immediate, but Mac's MachO64 object file format doesn't allow that for absolute addresses. [Unable to move variables in .data to registers with Mac x86 Assembly](https://stackoverflow.com/q/50205129). That is a total showstopper. – Peter Cordes Oct 26 '20 at 01:06
  • This code is inefficiently written for Linux: should have used `mov $str, %edi` if you're going to take advantage of 32-bit absolute addresses instead of using a RIP-relative `lea str(%rip), %rdi` that would work everywhere, except Windows where the calling convention differs. A native Windows program would need `lea str(%rip), %rcx` assuming a normal C library for `printf`, so presumably your fellow students with Windows are actually using a VM or WSL to get a Linux environment under Windows, if they're also not modifying the asm source. **What you need is Linux, not Windows.** – Peter Cordes Oct 26 '20 at 01:08
  • @MichaelPetch: I wonder if the `.rodata` section name is a problem for MacOS? Linux clang 10.0 assembles it just fine with the comment on my GNU/Linux desktop, so I'd be surprised if Apple's custom build of clang broke comment-parsing on `.section` lines. Like maybe it was still looking for a section name it liked when it got to the comment "token"? – Peter Cordes Oct 26 '20 at 01:11
  • @PeterCordes : he says he **can't modify** the code (not sure reason) so can't change `mov $str, %rdi` to `mov $str, %edi` so he'll get the typical _error in backend: 32-bit absolute addressing is not supported in 64-bit mode_ . As for `.section .rodata` problem it isn't comment. I forgot earlier the Apple LLVM clang compiler requires [`.section`](https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html) to contain at least 2 parameters, wth the first being the segment name and the second being the section name. – Michael Petch Oct 26 '20 at 01:30
  • You'd create a read only section in the __TEXT segment with a section name. Something like `.section __TEXT,__const` for example. – Michael Petch Oct 26 '20 at 01:43
  • 1
    @MichaelPetch: I should have phrased that comment better: *the professor* who wrote this code they're not allowed to modify should have used `mov $str, %edi` if they were going to use 32-bit absolute addresses in the first place (not `movabs` or RIP-relative `lea`). `movq` has zero advantages. My point was that this code can only work on Linux because 1. x86-64 System V calling convention. 2. using 32-bit absolute addresses. (Inefficiently). – Peter Cordes Oct 26 '20 at 01:43
  • 1
    Oh I am sure the other students were using WSL or something like that. Was why I suggested installing Linux in a VM and not Windows. – Michael Petch Oct 26 '20 at 01:47
  • 1
    @MichaelPetch: I supposed you could rename it `hello.S` and build it with `clang -Dmovq=movabs`, but that doesn't work for code that does `movq` between two registers. Or a `movq` with a memory destination. But a silly preprocessor hack doesn't fix other showstoppers like `.section`. And yeah, I came to the same WSL conclusion. I highlighted Linux not Windows in my answer because of other comments (not yours) that went along with the OP's idea of getting Windows. – Peter Cordes Oct 26 '20 at 01:48
  • Very similar question was asked (and answered) before https://stackoverflow.com/a/73159572/13212398 – AlexFreik Jul 29 '22 at 03:15

0 Answers0