Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
X86 Assembly/NASM Syntax 1
X86 Assembly/NASM Syntax
The Netwide Assembler is an x86 and x86-64 assembler that uses syntax similar to Intel. It supports a variety of
object file formats, including:
1.1. ELF32/64
2.2. Linux a.out
3.3. NetBSD/FreeBSD a.out
4.4. MS-DOS 16-bit/32-bit object files
5.5. Win32/64 object files
6.6. COFF
7.7. Mach-O 32/64
8.8. rdf
NASM runs on both Unix and Windows/DOS.
NASM Syntax
The Netwide Assembler (NASM) uses a syntax "designed to be simple and easy to understand, similar to Intel's but
less complex". This means that the operand order is dest then src, as opposed to the AT&T style used by the GNU
Assembler. For example,
mov ax, 9
loads the number 9 into register ax.
For those using gdb with nasm, you can set gdb to use Intel-style disassembly by issuing the command:
set disassembly-flavor intel
Comments
A single semi-colon is used for comments, and functions the same as double slash in C++: the compiler ignores from
the semicolon to the next newline.
Macros
NASM has powerful macro functions, similar to C's preprocessor. For example,
%define newline 0xA
%define func(a, b) ((a) * (b) + 2)
func (1, 22) ; expands to ((1) * (22) + 2)
%defmacro print 1 ; macro with one argument
push dword %1 ; %1 means first argument
call printf
add esp, 4
%endmacro
print mystring ; will call printf
X86 Assembly/NASM Syntax 2
Example I/O (Linux and BSD)
To pass the kernel a simple input command on Linux, you would pass values to the following registers and then send
the kernel an interrupt signal. To read in a single character from standard input (such as from a user at their
keyboard), do the following:
; read a byte from stdin
mov eax, 3 ; 3 is recognized by the system as meaning "read"
mov ebx, 0 ; read from standard input
mov ecx, variable ; address to pass to
mov edx, 1 ; input length (one byte)
int 0x80 ; call the kernel
After the int 0x80, eax will contain the number of bytes read. If this number is < 0, there was a read error of
some sort.
Outputting follows a similar convention:
; print a byte to stdout
mov eax, 4 ; the system interprets 4 as "write"
mov ebx, 1 ; standard output (print to terminal)
mov ecx, variable ; pointer to the value being passed
mov edx, 1 ; length of output (in bytes)
int 0x80 ; call the kernel
BSD systems (MacOS X included) use similar system calls, but convention to execute them is different. While on
Linux you pass system call arguments in different registers, on BSD systems they are pushed onto stack (except the
system call number, which is put into eax, the same way as in Linux). BSD version of the code above:
; read a byte from stdin
mov eax, 3 ; sys_read system call
push dword 1 ; input length
push dword variable ; address to pass to
push dword 0 ; read from standard input
push eax
int 0x80 ; call the kernel
add esp, 16 ; move back the stack pointer
; write a byte to stdout
mov eax, 4 ; sys_write system call
push dword 1 ; output length
push dword variable ; memory address
push dword 1 ; write to standard output
push eax
int 0x80 ; call the kernel
add esp, 16 ; move back the stack pointer
; quit the program
mov eax, 1 ; sys_exit system call
push dword 0 ; program return value
push eax
X86 Assembly/NASM Syntax 3
int 0x80 ; call the kernel
Hello World (Linux)
Below we have a simple Hello world example, it lays out the basic structure of a nasm program:
global _start
section .data
; Align to the nearest 2 byte boundary, must be a power of two
align 2
; String, which is just a collection of bytes, 0xA is newline
str: db 'Hello, world!',0xA
strLen: equ $-str
section .bss
section .text
_start:
;
; op dst, src
;
;
; Call write(2) syscall:
; ssize_t write(int fd, const void *buf, size_t count)
;
mov edx, strLen ; Arg three: the length of the string
mov ecx, str ; Arg two: the address of the string
mov ebx, 1 ; Arg one: file descriptor, in this case stdout
mov eax, 4 ; Syscall number, in this case the write(2) syscall:
int 0x80 ; Interrupt 0x80
;
; Call exit(3) syscall
; void exit(int status)
;
mov ebx, 0 ; Arg one: the status
mov eax, 1 ; Syscall number:
int 0x80
In order to assemble, link and run the program we need to do the following:
$ nasm -felf32 -g helloWorld.asm
$ ld -g helloWorld.o
$ ./a.out
X86 Assembly/NASM Syntax 4
Hello World (Using only Win32 system calls)
In this example we are going to rewrite the hello world example using Win32 system calls. There are several major
differences:
1.1. The intermediate file will be a Microsoft Win32 (i386) object file
2.2. We will avoid using interrupts since they may not be portable and therefore we need to bring in several calls from
kernel32 DLL
global _start
extern _GetStdHandle@4
extern _WriteConsoleA@20
extern _ExitProcess@4
section .data
str: db 'hello, world',0xA
strLen: equ $-str
section .bss
numCharsWritten: resb 1
section .text
_start:
;
; HANDLE WINAPI GetStdHandle( _In_ DWORD nStdHandle ) ;
;
push dword -11 ; Arg1: request handle for standard output
call _GetStdHandle@4 ; Result: in eax
;
; BOOL WINAPI WriteConsole(
; _In_ HANDLE hConsoleOutput,
; _In_ const VOID *lpBuffer,
; _In_ DWORD nNumberOfCharsToWrite,
; _Out_ LPDWORD lpNumberOfCharsWritten,
; _Reserved_ LPVOID lpReserved ) ;
;
push dword 0 ; Arg5: Unused so just use zero
push numCharsWritten ; Arg4: push pointer to numCharsWritten
push dword strLen ; Arg3: push length of output string
push str ; Arg2: push pointer to output string
push eax ; Arg1: push handle returned from _GetStdHandle
call _WriteConsoleA@20
;
; VOID WINAPI ExitProcess( _In_ UINT uExitCode ) ;
X86 Assembly/NASM Syntax 5
;
push dword 0 ; Arg1: push exit code
call _ExitProcess@4
In order to assemble, link and run the program we need to do the following. This example was run under cygwin, in
a Windows command prompt the link step would be different. In this example we use the -e command line option
when invoking ld to specify the entry point for program execution. Otherwise we would have to use
_WinMain@16 as the entry point rather than _start. One last note, WriteConsole() does not behave well
within a cygwin console, so in order to see output the final exe should be run within a Windows command prompt:
$ nasm -f win32 -g helloWorldWin32.asm
$ ld -e _start helloWorldwin32.obj -lkernel32 -o helloWorldWin32.exe
Hello World (Using C libraries and Linking with gcc)
In this example we will rewrite Hello World to use printf(3) from the C library and link using gcc. This has
the advantage that going from Linux to Windows requires minimal source code changes and a slightly different
assemble and link steps. In the Windows world this has the additional benefit that the linking step will be the same in
the Windows command prompt and cygwin. There are several major changes:
1. The "hello, world" string now becomes the format string for printf(3) and therefore needs to be null
terminated. This also means we do not need to explicitly specify it's length anymore.
2.2. gcc expects the entry point for execution to be main
3. Microsoft will prefix functions using the cdecl calling convention with a underscore. So main and printf
will become _main and _printf respectively in the Windows development environment.
global main
extern printf
section .data
fmtStr: db 'hello, world',0xA,0
section .text
main:
sub esp, 4 ; Allocate space on the stack for one 4 byte parameter
lea eax, [fmtStr]
mov [esp], eax ; Arg1: pointer to format string
call printf ; Call printf(3):
; int printf(const char *format, ...);
add esp, 4 ; Pop stack once
ret
In order to assemble, link and run the program we need to do the following.
X86 Assembly/NASM Syntax 6
$ nasm -felf32 helloWorldgcc.asm
$ gcc helloWorldgcc.o -o helloWorldgcc
The Windows version with prefixed underscores:
global _main
extern _printf ; Uncomment under Windows
section .data
fmtStr: db 'hello, world',0xA,0
section .text
_main:
sub esp, 4 ; Allocate space on the stack for one 4 byte parameter
lea eax, [fmtStr]
mov [esp], eax ; Arg1: pointer to format string
call _printf ; Call printf(3):
; int printf(const char *format, ...);
add esp, 4 ; Pop stack once
ret
In order to assemble, link and run the program we need to do the following.
$ nasm -fwin32 helloWorldgcc.asm
$ gcc helloWorldgcc.o -o helloWorldgcc

More Related Content

What's hot

Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
Wang Hsiangkai
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Frankie Jones
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
Srikrishnan Suresh
 
Assembly Language -I
Assembly Language -IAssembly Language -I
Assembly Language -I
Devika Rangnekar
 
Performance Characterization of the Pentium Pro Processor
Performance Characterization of the Pentium Pro ProcessorPerformance Characterization of the Pentium Pro Processor
Performance Characterization of the Pentium Pro Processor
Dileep Bhandarkar
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Space
ramanjosan
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
Abdullah Basheer
 
N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)
Selomon birhane
 
intro unix/linux 04
intro unix/linux 04intro unix/linux 04
intro unix/linux 04
duquoi
 
20 -miscellaneous
20  -miscellaneous20  -miscellaneous
20 -miscellaneous
Hector Garzo
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
duquoi
 
intro unix/linux 05
intro unix/linux 05intro unix/linux 05
intro unix/linux 05
duquoi
 
Macro
MacroMacro
Unit 3
Unit  3Unit  3
Unit 3
siddr
 
Orca share media1492336349924
Orca share media1492336349924Orca share media1492336349924
Orca share media1492336349924
Sudip Simkhada
 
Linker scripts
Linker scriptsLinker scripts
Linker scripts
Koganti Ravikumar
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
Plsql quick guide
Plsql quick guidePlsql quick guide
Plsql quick guide
1bi08me024
 
C from hello world to 010101
C from hello world to 010101C from hello world to 010101
C from hello world to 010101
Bellaj Badr
 

What's hot (20)

Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Assembly Language -I
Assembly Language -IAssembly Language -I
Assembly Language -I
 
Performance Characterization of the Pentium Pro Processor
Performance Characterization of the Pentium Pro ProcessorPerformance Characterization of the Pentium Pro Processor
Performance Characterization of the Pentium Pro Processor
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Space
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
 
N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)
 
intro unix/linux 04
intro unix/linux 04intro unix/linux 04
intro unix/linux 04
 
20 -miscellaneous
20  -miscellaneous20  -miscellaneous
20 -miscellaneous
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
intro unix/linux 05
intro unix/linux 05intro unix/linux 05
intro unix/linux 05
 
Macro
MacroMacro
Macro
 
Unit 3
Unit  3Unit  3
Unit 3
 
Orca share media1492336349924
Orca share media1492336349924Orca share media1492336349924
Orca share media1492336349924
 
Linker scripts
Linker scriptsLinker scripts
Linker scripts
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Plsql quick guide
Plsql quick guidePlsql quick guide
Plsql quick guide
 
C from hello world to 010101
C from hello world to 010101C from hello world to 010101
C from hello world to 010101
 

Similar to X86 assembly nasm syntax

NASM Introduction.pptx
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptx
AnshKarwa
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
shimosawa
 
Introduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingIntroduction to Assembly Language Programming
Introduction to Assembly Language Programming
Rahul P
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
Selomon birhane
 
Shellcoding in linux
Shellcoding in linuxShellcoding in linux
Shellcoding in linux
Ajin Abraham
 
Shellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse EngineeringShellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse Engineering
Sumutiu Marius
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
Jian-Yu Li
 
Linux Shellcode disassembling
Linux Shellcode disassemblingLinux Shellcode disassembling
Linux Shellcode disassembling
Harsh Daftary
 
Assem -lect-6
Assem -lect-6Assem -lect-6
Assem -lect-6
Dolly Angel
 
Buffer overflow – Smashing The Stack
Buffer overflow – Smashing The StackBuffer overflow – Smashing The Stack
Buffer overflow – Smashing The Stack
Tomer Zait
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
Hao-Ran Liu
 
Buffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the StackBuffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the Stack
ironSource
 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)
Selomon birhane
 
N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)
Selomon birhane
 
Ui disk & terminal drivers
Ui disk & terminal driversUi disk & terminal drivers
Ui disk & terminal drivers
Sarang Ananda Rao
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
Nora Youssef
 
Shellcoding, an Introduction
Shellcoding, an IntroductionShellcoding, an Introduction
Shellcoding, an Introduction
Daniele Bellavista
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
Partha Bhattacharya
 
7986-lect 7.pdf
7986-lect 7.pdf7986-lect 7.pdf
7986-lect 7.pdf
RiazAhmad521284
 
Alp 05
Alp 05Alp 05
Alp 05
gswapnil86
 

Similar to X86 assembly nasm syntax (20)

NASM Introduction.pptx
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptx
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Introduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingIntroduction to Assembly Language Programming
Introduction to Assembly Language Programming
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
 
Shellcoding in linux
Shellcoding in linuxShellcoding in linux
Shellcoding in linux
 
Shellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse EngineeringShellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse Engineering
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
 
Linux Shellcode disassembling
Linux Shellcode disassemblingLinux Shellcode disassembling
Linux Shellcode disassembling
 
Assem -lect-6
Assem -lect-6Assem -lect-6
Assem -lect-6
 
Buffer overflow – Smashing The Stack
Buffer overflow – Smashing The StackBuffer overflow – Smashing The Stack
Buffer overflow – Smashing The Stack
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Buffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the StackBuffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the Stack
 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)
 
N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)
 
Ui disk & terminal drivers
Ui disk & terminal driversUi disk & terminal drivers
Ui disk & terminal drivers
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Shellcoding, an Introduction
Shellcoding, an IntroductionShellcoding, an Introduction
Shellcoding, an Introduction
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
7986-lect 7.pdf
7986-lect 7.pdf7986-lect 7.pdf
7986-lect 7.pdf
 
Alp 05
Alp 05Alp 05
Alp 05
 

Recently uploaded

Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptxTale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
OH TEIK BIN
 
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry  by V.Jesinthal MaryPlato and Aristotle's Views on Poetry  by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
jessintv
 
What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17
Celine George
 
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docx
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docxQ1_LE_English 7_Lesson 1_Week 1 wordfile.docx
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docx
SANDRAMEMBRERE1
 
SD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptxSD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptx
MarkKennethBellen1
 
ACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docxACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docx
LeviMaePacatang1
 
Replacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdfReplacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdf
StefanMz
 
Understanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooksUnderstanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooks
TechSoup
 
Introduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptxIntroduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptx
jessintv
 
Bagong Pilipinas Pledge in Power pointpptx
Bagong Pilipinas Pledge in Power pointpptxBagong Pilipinas Pledge in Power pointpptx
Bagong Pilipinas Pledge in Power pointpptx
fantasialomibao
 
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
mariateresabadilla2
 
Email Marketing in Odoo 17 - Odoo 17 Slides
Email Marketing  in Odoo 17 - Odoo 17 SlidesEmail Marketing  in Odoo 17 - Odoo 17 Slides
Email Marketing in Odoo 17 - Odoo 17 Slides
Celine George
 
english 9 Quarter 1 Week 1 Modals and its Uses
english 9 Quarter 1 Week 1 Modals and its Usesenglish 9 Quarter 1 Week 1 Modals and its Uses
english 9 Quarter 1 Week 1 Modals and its Uses
EjNoveno
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
Celine George
 
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptxModule-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
MichelleMercado36
 
Module 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.pptModule 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.ppt
KIPAIZAGABAWA1
 
Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
Celine George
 
PPT Jessica powerpoint physical geography
PPT Jessica powerpoint physical geographyPPT Jessica powerpoint physical geography
PPT Jessica powerpoint physical geography
np2fjc9csm
 
A history of Innisfree in Milanville, Pennsylvania
A history of Innisfree in Milanville, PennsylvaniaA history of Innisfree in Milanville, Pennsylvania
A history of Innisfree in Milanville, Pennsylvania
ThomasRue2
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptxTale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
 
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry  by V.Jesinthal MaryPlato and Aristotle's Views on Poetry  by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
 
What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17
 
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docx
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docxQ1_LE_English 7_Lesson 1_Week 1 wordfile.docx
Q1_LE_English 7_Lesson 1_Week 1 wordfile.docx
 
SD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptxSD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptx
 
ACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docxACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docx
 
Replacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdfReplacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdf
 
Understanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooksUnderstanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooks
 
Introduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptxIntroduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptx
 
Bagong Pilipinas Pledge in Power pointpptx
Bagong Pilipinas Pledge in Power pointpptxBagong Pilipinas Pledge in Power pointpptx
Bagong Pilipinas Pledge in Power pointpptx
 
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
DO5s2024-Orientation-Material.pptx. This is a presentation of DepEd Order No....
 
Email Marketing in Odoo 17 - Odoo 17 Slides
Email Marketing  in Odoo 17 - Odoo 17 SlidesEmail Marketing  in Odoo 17 - Odoo 17 Slides
Email Marketing in Odoo 17 - Odoo 17 Slides
 
english 9 Quarter 1 Week 1 Modals and its Uses
english 9 Quarter 1 Week 1 Modals and its Usesenglish 9 Quarter 1 Week 1 Modals and its Uses
english 9 Quarter 1 Week 1 Modals and its Uses
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
 
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptxModule-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
 
Module 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.pptModule 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.ppt
 
Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
 
PPT Jessica powerpoint physical geography
PPT Jessica powerpoint physical geographyPPT Jessica powerpoint physical geography
PPT Jessica powerpoint physical geography
 
A history of Innisfree in Milanville, Pennsylvania
A history of Innisfree in Milanville, PennsylvaniaA history of Innisfree in Milanville, Pennsylvania
A history of Innisfree in Milanville, Pennsylvania
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
 

X86 assembly nasm syntax

  • 1. X86 Assembly/NASM Syntax 1 X86 Assembly/NASM Syntax The Netwide Assembler is an x86 and x86-64 assembler that uses syntax similar to Intel. It supports a variety of object file formats, including: 1.1. ELF32/64 2.2. Linux a.out 3.3. NetBSD/FreeBSD a.out 4.4. MS-DOS 16-bit/32-bit object files 5.5. Win32/64 object files 6.6. COFF 7.7. Mach-O 32/64 8.8. rdf NASM runs on both Unix and Windows/DOS. NASM Syntax The Netwide Assembler (NASM) uses a syntax "designed to be simple and easy to understand, similar to Intel's but less complex". This means that the operand order is dest then src, as opposed to the AT&T style used by the GNU Assembler. For example, mov ax, 9 loads the number 9 into register ax. For those using gdb with nasm, you can set gdb to use Intel-style disassembly by issuing the command: set disassembly-flavor intel Comments A single semi-colon is used for comments, and functions the same as double slash in C++: the compiler ignores from the semicolon to the next newline. Macros NASM has powerful macro functions, similar to C's preprocessor. For example, %define newline 0xA %define func(a, b) ((a) * (b) + 2) func (1, 22) ; expands to ((1) * (22) + 2) %defmacro print 1 ; macro with one argument push dword %1 ; %1 means first argument call printf add esp, 4 %endmacro print mystring ; will call printf
  • 2. X86 Assembly/NASM Syntax 2 Example I/O (Linux and BSD) To pass the kernel a simple input command on Linux, you would pass values to the following registers and then send the kernel an interrupt signal. To read in a single character from standard input (such as from a user at their keyboard), do the following: ; read a byte from stdin mov eax, 3 ; 3 is recognized by the system as meaning "read" mov ebx, 0 ; read from standard input mov ecx, variable ; address to pass to mov edx, 1 ; input length (one byte) int 0x80 ; call the kernel After the int 0x80, eax will contain the number of bytes read. If this number is < 0, there was a read error of some sort. Outputting follows a similar convention: ; print a byte to stdout mov eax, 4 ; the system interprets 4 as "write" mov ebx, 1 ; standard output (print to terminal) mov ecx, variable ; pointer to the value being passed mov edx, 1 ; length of output (in bytes) int 0x80 ; call the kernel BSD systems (MacOS X included) use similar system calls, but convention to execute them is different. While on Linux you pass system call arguments in different registers, on BSD systems they are pushed onto stack (except the system call number, which is put into eax, the same way as in Linux). BSD version of the code above: ; read a byte from stdin mov eax, 3 ; sys_read system call push dword 1 ; input length push dword variable ; address to pass to push dword 0 ; read from standard input push eax int 0x80 ; call the kernel add esp, 16 ; move back the stack pointer ; write a byte to stdout mov eax, 4 ; sys_write system call push dword 1 ; output length push dword variable ; memory address push dword 1 ; write to standard output push eax int 0x80 ; call the kernel add esp, 16 ; move back the stack pointer ; quit the program mov eax, 1 ; sys_exit system call push dword 0 ; program return value push eax
  • 3. X86 Assembly/NASM Syntax 3 int 0x80 ; call the kernel Hello World (Linux) Below we have a simple Hello world example, it lays out the basic structure of a nasm program: global _start section .data ; Align to the nearest 2 byte boundary, must be a power of two align 2 ; String, which is just a collection of bytes, 0xA is newline str: db 'Hello, world!',0xA strLen: equ $-str section .bss section .text _start: ; ; op dst, src ; ; ; Call write(2) syscall: ; ssize_t write(int fd, const void *buf, size_t count) ; mov edx, strLen ; Arg three: the length of the string mov ecx, str ; Arg two: the address of the string mov ebx, 1 ; Arg one: file descriptor, in this case stdout mov eax, 4 ; Syscall number, in this case the write(2) syscall: int 0x80 ; Interrupt 0x80 ; ; Call exit(3) syscall ; void exit(int status) ; mov ebx, 0 ; Arg one: the status mov eax, 1 ; Syscall number: int 0x80 In order to assemble, link and run the program we need to do the following: $ nasm -felf32 -g helloWorld.asm $ ld -g helloWorld.o $ ./a.out
  • 4. X86 Assembly/NASM Syntax 4 Hello World (Using only Win32 system calls) In this example we are going to rewrite the hello world example using Win32 system calls. There are several major differences: 1.1. The intermediate file will be a Microsoft Win32 (i386) object file 2.2. We will avoid using interrupts since they may not be portable and therefore we need to bring in several calls from kernel32 DLL global _start extern _GetStdHandle@4 extern _WriteConsoleA@20 extern _ExitProcess@4 section .data str: db 'hello, world',0xA strLen: equ $-str section .bss numCharsWritten: resb 1 section .text _start: ; ; HANDLE WINAPI GetStdHandle( _In_ DWORD nStdHandle ) ; ; push dword -11 ; Arg1: request handle for standard output call _GetStdHandle@4 ; Result: in eax ; ; BOOL WINAPI WriteConsole( ; _In_ HANDLE hConsoleOutput, ; _In_ const VOID *lpBuffer, ; _In_ DWORD nNumberOfCharsToWrite, ; _Out_ LPDWORD lpNumberOfCharsWritten, ; _Reserved_ LPVOID lpReserved ) ; ; push dword 0 ; Arg5: Unused so just use zero push numCharsWritten ; Arg4: push pointer to numCharsWritten push dword strLen ; Arg3: push length of output string push str ; Arg2: push pointer to output string push eax ; Arg1: push handle returned from _GetStdHandle call _WriteConsoleA@20 ; ; VOID WINAPI ExitProcess( _In_ UINT uExitCode ) ;
  • 5. X86 Assembly/NASM Syntax 5 ; push dword 0 ; Arg1: push exit code call _ExitProcess@4 In order to assemble, link and run the program we need to do the following. This example was run under cygwin, in a Windows command prompt the link step would be different. In this example we use the -e command line option when invoking ld to specify the entry point for program execution. Otherwise we would have to use _WinMain@16 as the entry point rather than _start. One last note, WriteConsole() does not behave well within a cygwin console, so in order to see output the final exe should be run within a Windows command prompt: $ nasm -f win32 -g helloWorldWin32.asm $ ld -e _start helloWorldwin32.obj -lkernel32 -o helloWorldWin32.exe Hello World (Using C libraries and Linking with gcc) In this example we will rewrite Hello World to use printf(3) from the C library and link using gcc. This has the advantage that going from Linux to Windows requires minimal source code changes and a slightly different assemble and link steps. In the Windows world this has the additional benefit that the linking step will be the same in the Windows command prompt and cygwin. There are several major changes: 1. The "hello, world" string now becomes the format string for printf(3) and therefore needs to be null terminated. This also means we do not need to explicitly specify it's length anymore. 2.2. gcc expects the entry point for execution to be main 3. Microsoft will prefix functions using the cdecl calling convention with a underscore. So main and printf will become _main and _printf respectively in the Windows development environment. global main extern printf section .data fmtStr: db 'hello, world',0xA,0 section .text main: sub esp, 4 ; Allocate space on the stack for one 4 byte parameter lea eax, [fmtStr] mov [esp], eax ; Arg1: pointer to format string call printf ; Call printf(3): ; int printf(const char *format, ...); add esp, 4 ; Pop stack once ret In order to assemble, link and run the program we need to do the following.
  • 6. X86 Assembly/NASM Syntax 6 $ nasm -felf32 helloWorldgcc.asm $ gcc helloWorldgcc.o -o helloWorldgcc The Windows version with prefixed underscores: global _main extern _printf ; Uncomment under Windows section .data fmtStr: db 'hello, world',0xA,0 section .text _main: sub esp, 4 ; Allocate space on the stack for one 4 byte parameter lea eax, [fmtStr] mov [esp], eax ; Arg1: pointer to format string call _printf ; Call printf(3): ; int printf(const char *format, ...); add esp, 4 ; Pop stack once ret In order to assemble, link and run the program we need to do the following. $ nasm -fwin32 helloWorldgcc.asm $ gcc helloWorldgcc.o -o helloWorldgcc