section .data prompt db 'Please input a number from 1 to 9, or type 0 to exit', 0Ah prompt_len equ $-prompt total db 0 ;Initialize total to 0 section .bss input resb 1 section .text global _start _start: repeat: ;SYS_WRITE (Display Prompt) mov eax, 4 ;Tell computer what call we are doing (4 represents SYS_WRITE) mov ebx, 1 ;Tell computer where to send output (send to STDOUT, console/window) mov ecx, prompt ;Tell computer where the data is that we want to print mov edx, prompt_len ;Tell computer how long out prompt string is int 80h ;Trigger interrupt, syscall happens, read our input ;SYS_READ (Read user input) mov eax, 3 ;Tell computer what call we're doing (3 represents sys_read) mov ebx, 0 ;Tell computer to read data from STDIN (console/input window) mov ecx, input ;Tell computer to put the data it reads into the input variable mov edx, 1 ;Tell computer how long our input will be int 80h ;Trigger interrupt, syscall happens, read our input ;Get our input into a register, then convert from ASCII into an integer mov al, [input] ;Put our 1 byte of data into the lower half of the AX register (AL) sub ebx, '0' ;Convert our ASCII char to an integer -- subtract 48 (char 0) from the value movzx ebx, al ;Move with Zero Extend, put AL's value into EBX (extend 8 bit value, fill the rest of bits with 0s in EBX) ;Check conditions to see if we should leave our loop ;Check if our input is == 0 cmp al, 0 je exit_loop mov al, [total] add al, [input] cmp al, 9 jg exit_loop ;If not exiting, add input to total mov al, [total] add al, [input] ;Repeat loop until conditions are met jmp repeat exit_loop: ;Before you print and exit, you MUST convert your total back into ASCII from an integer ;To do that, add ASCII 0's value (48, '0') to the total. mov al, [total] ;Put our 1 byte of data into the lower half of the AX register (AL) movzx ebx, al ;Move with Zero Extend, put AL's value into EBX (extend 8 bit value, fill the rest of bits with 0s in EBX) add ebx, '0' ;Convert our ASCII char to an integer -- subtract 48 (char 0) from the value ;SYS_WRITE syscall (Print the result) mov eax, 4 ;Tell computer what call we are doing (4 represents SYS_WRITE) mov ebx, 1 ;Tell computer where to send output (send to STDOUT, console/window) mov ecx, [total] ;Tell computer where the data is that we want to print *********CHANGE TO TOTAL!!!!!!********* mov edx, 1 ;Tell computer how long out prompt string is int 80h ;Exit the program ;Your program will stop running after this executes! mov eax, 1 ;sys_exit system call number mov ebx, 0 ;sys_exit, return 0 status on exit - 'No Errors' int 80h ;interrupt to invoke a system call (and exit the program)
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