Assembly language. Does anyone know how to?

Hey

I recently fell into an interest of learning assembly language.
The best way i guess is to learn by messing around with simple variables and "Hello World" type programs.

I tried a website tutorial that asked me to download an assembler/linker called FASM. I must have been doing something wrong because the tutorial seemed to make absolutely no sense to me and it got me nowhere.

Can anyone offer any advice, references, tutorials, suggestions? I really want to learn introductory level Assembly language.
IMO, the best way to learn assembly is to look at some very simple C code and figure out how each part works in assembly.
closed account (3hM2Nwbp)
To really "get" assembly, you'll need to learn the concepts behind how the system works.

Concepts that you should know (in no particular order, and most likely incomplete), include:

Control Structures - how do we control the flow of our program?
Memory - how is data stored?
Pointers - how do we access memory locations?
Registers - how do we actually perform calculations on values in our program?
Stacks - what do we keep on the stack, how is it arranged, and how do we access it?

After you have a solid understanding of minimally the above topics, it's time to start pulling your hair out experimenting.

You'll also have to pick a target architecture to learn - I'd recommend x86.
Last edited on
For serious Intel Assembly programming, look no further than MASM32
http://www.masm32.com/
This thread may help: http://cplusplus.com/forum/lounge/66135/

I need to read the PDF they link to at the bottom, but I've not had time to yet.
closed account (3TXyhbRD)
Most programming languages make the "Hello world!" project as their starting project, however Assembly is an exception. To print "Hello world!" to the screen you need to know about registers, segment addressing, interrupts. Also basic instructions for assigning values to registers. In other words, a lot of work before actually printing something.
Actually, what you do and what you don't need to know pretty much depends on what system you're working on. E.g. on our uni we worked with microcontrollers with displays that I believe were memory mapped.
I think hello world is actually pretty easy in most situations - all you need to know is how to declare a string, how to set up the string output system call, and how to terminate a program. Although yes, it includes assigning values to registers.

As a general advice: choose your platform, as Luc Lieber rightfully said.

Assembly is the language of the CPU, and each CPU architecture (sparc, mips, ppc, arm, x86, itanium, various microcontrollers) is completely different, only a few very generic architectural concepts are common.

Even within a given architecture, there may be major different options (16-bit realmode vs. 32-bit protmode vs x86_64, for example) and additional commands available (x86 just keeps rolling out those SSEs).

Besides the platform, choose the environment. You could be writing

* a freestanding program (bootloader/OS kernel), which has to do everything by directly accessing the hardware.

* kernel mode OS drivers or driver components, in which case you can access internal functions of the OS kernel to interact with it

* a usermode application that relies on the normal OS system calls to access hardware

* a module that is a part of a C or some other higher language project, in which case the C (or other language) standard library as well as all the other libraries linked with the project, are avaiable: you can printf() straight from assembly.

As a linux fan, I think linux assembly is pretty easy, and you can enjoy the convenience of all of its POSIX-style system calls without even linking the C library.
I like using Nasm under windows and ubuntu, but apparently windows uses something that ubuntu doesn't do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[section .data]
hello: db 'Hello, world!', 10, 0

[section .text]

	global _main
	extern _printf
	
_main:
	push hello
	call _printf
	add esp, 4
	
	mov eax, 0
	ret

In windows I can do:

nasm -f win32 file.asm -o file.obj
gcc file.obj -o file.exe


In ubuntu though I do:

nasm -f elf file.asm -o file.obj
gcc file.obj -o file

Windows works no complaints, and file.exe prints Hello, World. While ubuntu gives a lot of errors about no reference to _main or _printf calls.
@BHXSpecter: you need to link against the C runtime so that there is something that calls your _main, and you need to link against the C library so that there is a _printf for you to call... on second thought, gcc's linker should have done that for you. Just rename _main to main and _printf to printf.

On Linux, it's easy to skip the whole C business and have an assembly-only program.

Here's GNU as version, for x86_64 linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    .globl _start
_start:
    movq    $1, %rax  #  __NR_write 1
    movq    $1, %rdi  #  stdout
    movq    $msg, %rsi
    movq    $msglen, %rdx
    syscall

    xorq    %rdi, %rdi # return code will be 0
    movq    $60, %rax  # __NR_exit 60
    syscall
    .section .rodata
msg: .ascii  "Hello, world!\n"
msglen = .-msg


$ as -o test.o test.s
$ ld -o test test.o
$ ./test
Hello, world!

Last edited on
Topic archived. No new replies allowed.