tags:

views:

403

answers:

6

I'd like to play with writing some assembly on my Mac, ideally native, but I'd understand if it's easier to learn in QEMU or something.

I see that there are different dialects of assembly depending on the processor - what dialect is the "best" to learn?

I don't really have any idea of where to start, any pointers as to how to even run a program in assembly?

A: 

Well even though it isn't what some people call the "best" assembly out there, I would recommend learning X86 / X86-64 as it is the most widely used. To run the program, you can simply use GCC to translate it into binary and then run it through your console.

Gab Royer
+5  A: 

My suggestion is to act with the best assembler producer out there: gcc.

Write simple programs in C and then compile them with the -S switch. you will get a file.s containing the assembler code. Tinker with it and you will learn it. The best part is that if you want to learn a different assembler, you can just compile gcc as cross compiler, and produce assembler for any supported platform.

Remember to disable optimizations with -O0, otherwise you could find strange tricks.

This is hello world in assembler

    .cstring
LC0:
    .ascii "hello world!\0"
    .text
.globl _main
_main:
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ebx
    subl    $20, %esp
    call    L3
"L00000000001$pb":
L3:
    popl    %ebx
    leal    LC0-"L00000000001$pb"(%ebx), %eax
    movl    %eax, (%esp)
    call    L_puts$stub
    movl    $0, %eax
    addl    $20, %esp
    popl    %ebx
    leave
    ret
    .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
L_puts$stub:
    .indirect_symbol _puts
    hlt ; hlt ; hlt ; hlt ; hlt
    .subsections_via_symbols
Stefano Borini
That sounds a useful way of doing it. How do I turn the .s file into a binary?
Rich Bradshaw
with gcc hello.s :)
Stefano Borini
Does -s keep the debugging informations (variable name etc.)?
Gab Royer
Ah, apparently the -s switch is obsolete and is being ignored!
Rich Bradshaw
@Gab It does not seems so, as you can see on the code above. If there's a way, I don't know it.
Stefano Borini
@Rich: it's obsolete for the linker. add also -c to do only the compilation step and not the linking (you don't have any object code to link)
Stefano Borini
OK, you need -S not -s.
Rich Bradshaw
Yeah, that works as well
Stefano Borini
Or Fpc -al which puts the sourcelines as comment in the assembler
Marco van de Voort
+2  A: 

A few (OK many) years ago I picked up Peter Norton's book on Assembly http://www.amazon.com/Assembly-Language-Primer-Personal-Computer/dp/0136619010. It was a fantastic way to begin assembly programming for the PC. I wonder if you can still get this and use DOSbox to work through it.

rich
+5  A: 
DrJokepu
Just been reading the first book - it's good!
Rich Bradshaw
A: 

I learned MIPS and tested in SPIM for a class on "computer organization."

Not sure if this book will work, but it might be worth a shot. We used a "book" (more like a tutorial, and a cheatsheet really) written by the professor, so it's otherwise unavailable.

nullArray
A: 

Get involved with communities that thrive in assembly code. The first that comes to mind is the demoscene. For one, check out the productions released for x86 Macs on pouet.net. Try getting in touch with some of the coders in the active groups. Some of them may be willing to share ideas and code and maybe even get you motivated to write your own.

This is how I got started back in the day, but on an Atari 8-bit.

spoulson