-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnasm.txt
91 lines (71 loc) · 2.31 KB
/
nasm.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
section .data
; eg: myName db "Shreyas",10,0; 0 is null, 10 is \n
section .text
global _start
start:
;
;
;
;
-------------------------------------------------------
---------------------------------------------------------
SysCalls: ID,1,2,3,4,5,6 -> rax,rdi,rsi,rdx,r10,r8,r9
1. sys_read -> 0, #filedescriptor, $buffer, #count,-,-,-
2. sys_write-> 1, #filedescriptor, $buffer, #count,-,-,-
0 1 2 loc of string no.of bytes
i/p o/p err eg: myName
3.sys_exit
mov rax, 60 |
mov rdi, 0 |--> to exit from program
syscall |
----------------------------------------
--------------------------------------------------------------------------------------------
REGISTERS:
rip (eip, ip)64bit reg-index pointer //holds addr of next instruction; inc by 1 to move to next
rsp(esp, sp) - stack pointer
rbp - base pointer
r0 = rax,r1 = rcx ,r2 = rdx, r3 = rbx, r4 = rsp, r5 = rbp, r6 = rsi, r7 = rdi,
r8,r9,r10,r11,r12,r13,r14,r15
r0d = eax (lower 32 bits)
r0w = ax (lower 16 bits)
r0b = al (lower 8 bits)
ah,ch,dh,bh -> bits 15 through 8 of r0,r1,r2,r3
------------------------------------------------
---------------------------------------------------
INSTRUCTIONS:
cmp rax, rbx ; compare
je, jne, jg, jge, jl, jle, jz, jnz, jo(overflow), jno, js(signed), jns
call labelName; function call
...
labelName : ;
;
;
ret
mul reg; rax = rax * reg
imul reg ; signed mul
div reg; rax = rax/reg
neg reg; reg = -reg
inc reg;
dec reg;
adc a,b; a = a+b+CF
sbc a,b; a = a-b-CF
------------------------------------------------
-------------------------------------------------
STACK:
push reg/value; push
pop reg; pop
pop [addr]; pop to memory loc addr
mov reg, [rsp]; peek top of stack and store it in reg
-----------------------------------------------------
----------------------------------------------------------------
MACROS:
%macro addDigits 2; 2 is argc (no. of arguments)
mov rax, %1; %1 is the first parameter
mov rbx, %2; %2 is the second parameter
add rax,rbx;
STDIN equ 0; sim to #define
STDOUT equ 1
STDERR equ 2
SYS_READ equ 0
SYS_WRITE equ 1
SYS_EXIT equ 60