Reading ASM file Basics

advertisement
Quick Reference to read ASM code
[using NASM Assembler for x86 processors]:
Author: Surya Teja Paruchuri.
Date: July 13 2018.
Abstract:
This document is meant as a quick reference guide for assembly programming (for my use) and to get
started reading an .asm source (assembly source code). This is by no means comprehensive as I already
have some previous familiarity with assembly programming. (Thus any reader of this document is
expected to use any external reference by all means to get a complete details).
1. 3 sections in ASM file:
a. Data: For storing constant data types. (static memory section)
b. BSS: for variables which are (uninitialized), (static memory section).
c. Tex: Where Code resides.
2. Comments: ‘anything followed by ;’
3. Assembly language statements:
a. Executable instructions
b. Assembler directives or pseudo-ops
c. Macros.
4. Syntax of each line in ASM file:
a. [LABEL] mnemonic [operands] [; comments]
5. Hello World Program in assembly:
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.
l.
m.
n.
o.
p.
section
.text
global _start
_start:
mov
mov
mov
mov
int
mov
int
;must be declared for linker (ld)
edx,len
ecx,msg
ebx,1
eax,4
0x80
;tells linker entry point
;message length
;message to write
;file descriptor (stdout)
;system call number (sys_write)
;call kernel
eax,1
0x80
;system call number (sys_exit)
;call kernel
section
.data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg
;length of the string
6. Sections in the above program can be replaced with segments as follows:
a. segment .text
b.
global_start
c.
d. _start:
e.
mov edx,len
f.
mov ecx,msg
g.
mov ebx,1
;code segment
;must be declared for linker
;tell linker entry point
;message length
;message to write
;file descriptor (stdout)
h.
mov eax,4
;system call number (sys_write)
i.
int 0x80
;call kernel
j.
k.
mov eax,1
;system call number (sys_exit)
l.
int 0x80
;call kernel
m.
n. segment .data
;data segment
o. msg db 'Hello, world!',0xa
;our dear string
p. len equ
$ - msg
;length of our dear string
7. Memory segments:
a. Data Segments: .data, .bss
b. Code Segment: .text
c. Stack.
8. Registers Types:
a. Processor Registers Types:
i. General Registers Types:
1. Data Registers- typically contains:
a. A- Accumulator
b. B- Base Register for indexed addressing.
c. C- Count Register for looping.
d. D- Data Register.
2. Pointer Registers- typically contains:
a. IP- Instruction Pointer- stores the offset of next Instruction word
to be executed.
b. Stack Pointer (SP)- Offset value within the program stack.
c. Base Pointer (BP)- Base Pointer register helps in referencing the
parameters passed to the function.
3. Index Registers- typically contains:
a. Source Index (SI)- source index for string operations.
b. Destination Index (DI) – destination index for string operations.
ii. Control Register: contains flags. Typically includes:
1. Over flag
2. Interrupt flag
3. Trap flag (for single-step debugging)
4. Sign flag
5. Zero flag
6. Auxiliary carry flag
7. Parity flag
8. Carry flag
iii. Segment Registers:
1. Code Segment Register: contains address where code section of the
program begins
2. Data Segment Register: contains address where data section of the
program begins.
3. Stack segment: Data and return addresses of the procedures.
9. There are special Interrupt codes for calling the System Functions.
10. Addressing Mode:
a. Register Addressing: Register contains the operand.
b. Immediate Addressing: Where 1 operand is constant. In case 1 operand is constant, first
of the two operands passed to instruction must be a register.
c. Direct Addressing Mode: Directly access the data by modifying the address directly.
d. In Direct Addressing Modes: Utilizes Register-B (Base), Register-P (pointers) along with
DI or SI.
11. MOV dst, src
12. Allocate Storage (for initialized variables):
a. Syntax is:
i. [variable name]
define-directive
initial-value
[,initial value…,]
b. define-directives:
i. DB
ii. DW
iii. DD
iv. DQ – define Quad Bytes
v. DT - define ten Bytes
13. Allocate Storage for (Uninitialized variables): syntax is same as above.
a. Define -directives:
i. RESB – reserve a Byte.
ii. RESW – reserve a Word.
iii. RESD – reserve a Double Word.
iv. RESQ
v. REST
14. We can store string lengths directly using $ as shown below:
a. msg
b. len
db 'Hello, world!',0xa ;our dear string
equ $ - msg
;length of our dear string
15. More example String Instructions:
a. MOVS
b. LODS
c. STOS
d. CMPS
e. SCAS
16. Prodecures/Routines:
a. call < Procedure Label> - to call Procedure.
b. ret - to return from the Procedure.
c. After the call line, move the returned value from res register.
d. PUSH, POP to push and pop values too stack.( The stacks are in the stack segment of
memory).
17. Anything that starts with a ‘.’ is an assembler directive.
Reference:
[1]. https://www.tutorialspoint.com/assembly_programming/index.htm
Download