section .data MsgYes dw 'Yes' MsgNo dw 'No' section .text global _start _start: call readNum mov rsi, MsgYes mov rbx , rax inc rbx shr rbx,2 cmp rax , 2 jz code mov rcx,1 mov rsi, MsgNo cmp rax , 1 jz code lop: inc rcx push rax div rcx cmp rdx,0 je code xor rdx,rdx pop rax cmp rbx,rcx jne lop mov rsi,MsgYes code: call printString Exit: mov rax, sys_exit xor rdi, rdi syscall %ifndef SYS_EQUAL %define SYS_EQUAL sys_read equ 0 sys_write equ 1 sys_open equ 2 sys_close equ 3 sys_lseek equ 8 sys_create equ 85 sys_unlink equ 87 sys_mkdir equ 83 sys_makenewdir equ 0q777 sys_mmap equ 9 sys_mumap equ 11 sys_brk equ 12 sys_exit equ 60 stdin equ 0 stdout equ 1 stderr equ 3 PROT_NONE equ 0x0 PROT_READ equ 0x1 PROT_WRITE equ 0x2 MAP_PRIVATE equ 0x2 MAP_ANONYMOUS equ 0x20 ;access mode O_DIRECTORY equ 0q0200000 O_RDONLY equ 0q000000 O_WRONLY equ 0q000001 O_RDWR equ 0q000002 O_CREAT equ 0q000100 O_APPEND equ 0q002000 BEG_FILE_POS equ 0 CURR_POS equ 1 END_FILE_POS equ 2 ; create permission mode sys_IRUSR equ 0q400 ; user read permission sys_IWUSR equ 0q200 ; user write permission NL equ 0xA Space equ 0x20 %endif %ifndef NOWZARI_IN_OUT %define NOWZARI_IN_OUT ;---------------------------------------------------- newLine: push rax mov rax, NL call putc pop rax ret ;--------------------------------------------------------- putc: push rcx push rdx push rsi push rdi push r11 push ax mov rsi, rsp ; points to our char mov rdx, 1 ; how many characters to print mov rax, sys_write mov rdi, stdout syscall pop ax pop r11 pop rdi pop rsi pop rdx pop rcx ret ;--------------------------------------------------------- writeNum: push rax push rbx push rcx push rdx sub rdx, rdx mov rbx, 10 sub rcx, rcx cmp rax, 0 jge wAgain push rax mov al, '-' call putc pop rax neg rax wAgain: cmp rax, 9 jle cEnd div rbx push rdx inc rcx sub rdx, rdx jmp wAgain cEnd: add al, 0x30 call putc dec rcx jl wEnd pop rax jmp cEnd wEnd: pop rdx pop rcx pop rbx pop rax ret ;--------------------------------------------------------- getc: push rcx push rdx push rsi push rdi push r11 sub rsp, 1 mov rsi, rsp mov rdx, 1 mov rax, sys_read mov rdi, stdin syscall mov al, [rsi] add rsp, 1 pop r11 pop rdi pop rsi pop rdx pop rcx ret ;--------------------------------------------------------- readNum: push rcx push rbx push rdx mov bl,0 mov rdx, 0 rAgain: xor rax, rax call getc cmp al, '-' jne sAgain mov bl,1 jmp rAgain sAgain: cmp al, NL je rEnd cmp al, ' ' ;Space je rEnd sub rax, 0x30 imul rdx, 10 add rdx, rax xor rax, rax call getc jmp sAgain rEnd: mov rax, rdx cmp bl, 0 je sEnd neg rax sEnd: pop rdx pop rbx pop rcx ret ;------------------------------------------- printString: push rax push rcx push rsi push rdx push rdi mov rdi, rsi call GetStrlen mov rax, sys_write mov rdi, stdout syscall pop rdi pop rdx pop rsi pop rcx pop rax ret ;------------------------------------------- ; rdi : zero terminated string start GetStrlen: push rbx push rcx push rax xor rcx, rcx not rcx xor rax, rax cld repne scasb not rcx lea rdx, [rcx -1] ; length in rdx pop rax pop rcx pop rbx ret ;------------------------------------------- %endif
Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly
and start coding.
Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.
Every assembler may have it's own assembly language designed for a specific computers or an operating system.
Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start
specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
Define Directive | Description | Allocated Space |
---|---|---|
DB | Define Byte | 1 byte |
DW | Define Word | 2 bytes |
DD | Define Doubleword | 4 bytes |
DQ | Define Quadword | 8 bytes |
DT | Define Ten Bytes | 10 bytes |
Define Directive | Description |
---|---|
RESB | Reserve a Byte |
RESW | Reserve a Word |
RESD | Reserve a Doubleword |
RESQ | Reserve a Quadword |
REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
Loops are used to iterate a set of statements for a specific number of times.
mov ECX,n
L1:
;<loop body>
loop L1
where n specifies the no of times loops should iterate.
Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
procedure_name:
;procedure body
ret