0% found this document useful (0 votes)
39 views36 pages

Beginner's Guide to Assembly Language

This document serves as a beginner's guide to assembly language, covering essential concepts such as registers, stack frames, and system calls. It provides examples of assembly code, explains how to generate assembly output from compiled code, and discusses the use of assembly in various programming contexts. Additionally, it includes references for further reading on x86 assembly programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views36 pages

Beginner's Guide to Assembly Language

This document serves as a beginner's guide to assembly language, covering essential concepts such as registers, stack frames, and system calls. It provides examples of assembly code, explains how to generate assembly output from compiled code, and discusses the use of assembly in various programming contexts. Additionally, it includes references for further reading on x86 assembly programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A Beginners

guide to
Assembly
By Rishiraj , Varun and Shreyas

1
2
3
4
5
6
.out file on Linux
.exe on Windows

7
Our
Focus

8
Prominent ISAs

9
10
An intriguing Example!

11
Some Basics
● % - indicates register names. Example : %rbp

● $ - indicates constants Example : $100

● Accessing register values:


○ %rbp : Access value stored in register rbp

○ (%rbp) : Treat value stored in register rbp as a pointer. Access


the value stored at address pointed by the pointer. Basically *rbp

○ 4(%rbp) : Access value stored at address which is 4 bytes after


the address stored in rbp. Basically *(rbp + 4)
12
An intriguing Example!

13
An intriguing Example!

For each function call, new space is created on the stack to store
local variables and other data. This is known as a stack frame. To
accomplish this, you will need to write some code at the beginning
and end of each function to create and destroy the stack frame
14
An intriguing Example!

rbp is the frame pointer. In our code, it gets a snapshot of the stack
pointer (rsp) so that when rsp is changed, local variables and
function parameters are still accessible from a constant offset from
rbp.
15
An intriguing Example!

move immediate value 3000 to (%rbp-8)

16
An intriguing Example!

add immediate value 3 to (%rbp-8)

17
An intriguing Example!

Move immediate value 100 to (%rbp-4)

18
An intriguing Example!

Move (%rbp-4) to auxiliary register

19
An intriguing Example!

Pop the base pointer to restore state

20
An intriguing Example!

The calling convention dictates that a function’s return value is stored in


%eax, so the above instruction sets us up to return y at the end of our
function.
21
Operation Suffixes

● b = byte (8 bit)
● s = single (32-bit floating point)
● w = word (16 bit)
● l = long (32 bit integer or 64-bit floating point)
● q = quad (64 bit)
● t = ten bytes (80-bit floating point)

22
How to get assembly code?
Two ways:

● While Compiling
○ Use -S flag with gcc. WIll create a .s file
containing assembly

● Using Binary
○ Use objdump. Will show the assembly in terminal.

23
Understanding the output
● The output will have assembly, but there is more information!
● You will see lots of Directives like:
○ .file
○ .text
○ .global name

24
Understanding the output
● The output will have assembly, but there is more information also!.
● You will see lots of Directives like:
○ .file
○ .text
○ .global name

25
x86 Register Set

26
x86 Register Set : A few more

● Registers starting with “r”


○ Same as “e” registers but 64 bits wide

● EIP : The Instruction Pointer or the Program Counter

27
An Example with Loops!

28
System Calls in Assembly
kernel:
int 80h ; //Call kernel
ret

open:
push dword mode
push dword flags
push dword path
mov eax, 5
call kernel
add esp, byte 12
ret

29
System Calls in Assembly
kernel:
int 80h ; //Call kernel
ret

open:
push dword mode
push dword flags
push dword path
mov eax, 5 Syscall Number
call kernel
add esp, byte 12
ret

30
A bit different!

A simple fork program

31
Embedding Assembly in C

__asm__( “instruction 1”, “instruction 2”, ...)

Example:

__asm__(
"movl %edx, %eax\n\t"
"addl $2, %eax\n\t"
);
32
Embedding Assembly in C

33
Where will I use assembly?

34
Where will I use assembly?

● To write Compilers and Device Drivers

● To write viruses and for malware analysis

● Used while programming Real Time Embedded systems

● Implementing Locks for Concurrency.


We will cover this in the third module of the course!

35
References

● Chapter 11. x86 Assembly Language Programming, FreeBSD,


[Link]

● Easy x86-64, [Link]

● Introduction to the GNU/Linux assembler and linker for Intel Pentium processors,
[Link]

● Is there a way to insert assembly code into C?,


[Link]
o-c

36

You might also like