|
|
| $ | nasm -f elf XXX.asm | ; for producing XXX.o ELF object file | |
| $ | ld -s -o XXX XXX.o | ; for producing XXX executable | |
| $ | exact_path_from_root/XXX | ; for running(executing) Ex. /home/guest/XXX |
| label | inst | operand | comments | |
| ;Example Program | ||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
| section .data | ||||
| msg | db | "I'm a true hacker",0xa | ;String to be printed | |
| len | equ | $ - msg | ;Length of the string | |
| section .text | ||||
| global _start | ; we must export the entry point to the ELF linker or loader. | |||
| ;They conventionally recognize _start as their entry point. Use ld -e foo to override the default. | ||||
| _start: | ||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
| mov | edx, len | ;third argument: message length | ||
| mov | ecx, msg | ;second argument: pointer to message to write | ||
| mov | ebx, 1 | ;first argument: file handle(stdout) | ||
| mov | eax, 4 | ;system call number (sys_write) | ||
| int | 0x80 | ; call kernel | ||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
| mov | ebx, 0 | |||
| mov | eax, 1 | ;system call number (sys_exit) | ||
| int | 0x80 | |||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| label | inst | operand | comments | |
| ;Program to print the sum of two integers; sum should be less than 65536 | ||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
| section .data | ||||
| buf | db | 4 | ;Declaring a buffer | |
| section .data | ||||
| crlf | db | 0x0D0A | ||
| section .text | ||||
| global _start | ; we must export the entry point to the ELF linker or loader. | |||
| ;They conventionally recognize _start as their entry point. Use ld -e foo to override the default. | ||||
| _start: | ||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
| mov | ax, 65530 | ;moving the first number into the accumulator | ||
| mov | bx, 5 | ;moving the second number into the bx register | ||
| add | ax,bx | ;adding the two nuumbers & the sum to remain in the accumulator | ||
| mov | bx,10 | ; | ||
| mov | si,0 | ; | ||
| disp1: | mov | dx,0 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| div | bx | |||
| push | dx | ; | ||
| inc | si | |||
| or | ax,ax | |||
| jnz | disp1 | |||
| disp2 | pop | dx | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| add | dl,'0' | |||
| mov | [buf],dl | |||
| mov | edx,1 | |||
| mov | ecx,buf | |||
| mov | ebx,1 | |||
| mov | eax,4 | |||
| int | 80h | |||
| dec | si | |||
| jnz | disp2 | |||
| mov | edx,1 | |||
| mov | ecx,crlf | |||
| mov | ebx,1 | |||
| mov | eax,4 | |||
| int | 0x80 | |||
| mov | ebx,0 | |||
| mov | eax,1 | |||
| int | 80h |