Example: The following program creates and open a file named myfile.txt, and writes a text 'Welcome to Tutorials Point' in this file. Next the program reads from the file and stores the data into a buffer named info. Lastly it displays the text as stored in info. section .text global main ;must be declared for using gcc main: ;tell linker entry point ;create the file mov eax, 8 mov ebx, file_name mov ecx, 0777 ;read, write and execute by all int 0x80 ;call kernel mov [fd_out], byte eax ; write into the file mov edx,len mov ecx, msg mov ebx, [fd_out] mov eax,4 int 0x80 ;number of bytes ;message to write ;file descriptor ;system call number (sys_write) ;call kernel ; close the file mov eax, 6 mov ebx, [fd_out] ; write the message indicating end of file write mov eax, 4 mov ebx, 1 mov ecx, msg_done mov edx, len_done int 0x80 ;open the file for reading mov eax, 5 mov ebx, file_name mov ecx, 0 ;for read only access mov edx, 0777 ;read, write and execute by all int 0x80 mov [fd_in], byte eax ;read from file mov eax, 3 mov ebx, [fd_in] mov ecx, info mov edx, 26 int 0x80 ; close the file mov eax, 6 mov ebx, [fd_in] ; print the info mov eax, 4 mov ebx, 1 mov ecx, info mov edx, 26 int 0x80 mov eax,1 TUTORIALS POINT Simply Easy Learning ;system call number (sys_exit)
int 0x80 ;call kernel section .data file_name db 'myfile.txt' msg db 'Welcome to Tutorials Point' len equ $-msg msg_done db 'Written to file', 0xa len_done equ $-msg_done section .bss fd_out resb 1 fd_in resb 1 info resb 26 When the above code is compiled and executed, it produces following result: Written to file Welcome to Tutorials Point TUTORIALS POINT Simply Easy Learning
- Page 1 and 2:
Assembly Language Tutorial
- Page 3 and 4:
ABOUT THE TUTORIAL Assembly Program
- Page 5 and 6:
Table of Content Assembly Programmi
- Page 7 and 8:
The XOR Instruction ...............
- Page 9 and 10:
CHAPTER 1 Assembly Introduction Wha
- Page 11 and 12:
Main use of hexadecimal numbers in
- Page 13 and 14:
x: memory address When the processo
- Page 15 and 16:
Check The netwide assembler (NASM)
- Page 17 and 18:
; This program displays a message o
- Page 19 and 20:
CHAPTER 4 Assembly Memory Segments
- Page 21 and 22:
CHAPTER 5 Assembly Registers Proces
- Page 23 and 24:
Control Registers The 32-bit instru
- Page 25 and 26:
CHAPTER 6 Assembly System Calls Sys
- Page 27 and 28: Please enter a number: 1234 You hav
- Page 29 and 30: Direct Memory Addressing When opera
- Page 31 and 32: int 0x80 ;call kernel mov eax,1 ;sy
- Page 33 and 34: Processor uses the little-endian by
- Page 35 and 36: CHAPTER 9 Assembly Constants There
- Page 37 and 38: The above code replaces PTR by [EBP
- Page 39 and 40: dec [value] mov ebx, count inc word
- Page 41 and 42: int 0x80 When the above code is com
- Page 43 and 44: global main ;must be declared for u
- Page 45 and 46: mov [res], ax mov ecx,msg mov edx,
- Page 47 and 48: AND AL, 01H ; ANDing with 0000 0001
- Page 49 and 50: The TEST Instruction The TEST instr
- Page 51 and 52: L7: ... CMP is often used for compa
- Page 53 and 54: jg _exit mov ecx, [num3] _exit: mov
- Page 55 and 56: section .bss num resb 1 mov ecx, nu
- Page 57 and 58: Such conversions are however, has a
- Page 59 and 60: mov ecx,msg ;message to write mov e
- Page 61 and 62: LODS - This instruction loads from
- Page 63 and 64: mov ecx, len mov esi, s1 mov edi, s
- Page 65 and 66: found! Repetition Prefixes The REP
- Page 67 and 68: Example: The following example demo
- Page 69 and 70: mov ecx, res mov edx, 1 mov ebx, 1
- Page 71 and 72: CHAPTER 18 Assembly Recursion Arecu
- Page 73 and 74: CHAPTER 19 Assembly Macros Writing
- Page 75 and 76: CHAPTER 20 Assembly File Management
- Page 77: Put the buffer size, i.e., the numb
- Page 81: mov eax, 1 xor ebx, ebx int 80h sec