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) 
by

Assembly Online Compiler

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.

About Assembly

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.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

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.

Procedures

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