Assembly – Addition and Subtraction

email me

< back

Tested using JDoodle.

See multiplication. Learning: 1  2  3  4  5  6  book

;SINGLE DIGIT ADDITION
;test here: https://www.jdoodle.com/compile-assembler-nasm-online

section	.text
   global _start        
	
_start:	                ;entry point
   mov	eax, '4'
   sub  eax, '0'
	
   mov 	ebx, '5'
   sub  ebx, '0'
   add 	eax, ebx
   add	eax, '0'
	
   mov 	[sum], eax
   mov	ecx, msg	
   mov	edx, len
   mov	ebx, 1	         ;file descriptor (stdout)
   mov	eax, 4	         ;system call number (sys_write)
   int	0x80	         ;call kernel
	
   mov	ecx,sum
   mov	edx, 1
   mov	ebx, 1	         ;ile descriptor (stdout)
   mov	eax, 4	         ;system call number (sys_write)
   int	0x80	         ;call kernel
	
   mov	eax, 1	         ;system call number (sys_exit)
   int	0x80	         ;call kernel
	
section .data
msg db "The sum is:", 0xA, 0xD 
len equ $ - msg   
segment .bss
sum resb 1

;;;;output: 9
;DOUBLE DIGIT ADDITION
;test here: https://www.jdoodle.com/compile-assembler-nasm-online

section .text
global _start

_start: ;entry point

mov esi, 1         ;right digit minus 1 placement. Example: 23 would be 2.
mov ecx, 2         ;total digits. Example 48 would be 2.
clc
add_loop:
mov al, [num1 + esi]
adc al, [num2 + esi]
aaa
pushf
or al, 30h
popf

mov [sum + esi], al
dec esi
loop add_loop

mov edx, len       ;message length
mov ecx, msg       ;message to write
mov ebx, 1         ;file descriptor (stdout)
mov eax, 4         ;system call number (sys_write)
int 0x80           ;call kernel

mov edx, 5         ;message length
mov ecx, sum       ;message to write
mov ebx, 1         ;file descriptor (stdout)
mov eax, 4         ;system call number (sys_write)
int 0x80           ;call kernel

mov eax, 1         ;system call number (sys_exit)
int 0x80           ;call kernel

section .data
msg db 'The Sum is:', 0xa
len equ $ - msg
num1 db '12'
num2 db '23'
sum db ' '

;;;;output: 35
;SINGLE DIGIT ADDITION - ACCEPT USER INPUT
;test here: https://www.jdoodle.com/compile-assembler-nasm-online

SYS_EXIT  equ 1
SYS_READ  equ 3
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1

segment .data 

   msg1 db "Enter a digit ", 0xA, 0xD 
   len1 equ $- msg1 

   msg2 db "Please enter a second digit", 0xA, 0xD 
   len2 equ $- msg2 

   msg3 db "The sum is: "
   len3 equ $- msg3

segment .bss

   num1 resb 2 
   num2 resb 2 
   res resb 1    

section	.text
   global _start    
	
_start:             ;entry point
   mov eax, SYS_WRITE         
   mov ebx, STDOUT         
   mov ecx, msg1         
   mov edx, len1 
   int 0x80                

   mov eax, SYS_READ 
   mov ebx, STDIN  
   mov ecx, num1 
   mov edx, 2
   int 0x80            

   mov eax, SYS_WRITE        
   mov ebx, STDOUT         
   mov ecx, msg2          
   mov edx, len2         
   int 0x80

   mov eax, SYS_READ  
   mov ebx, STDIN  
   mov ecx, num2 
   mov edx, 2
   int 0x80        

   mov eax, SYS_WRITE         
   mov ebx, STDOUT         
   mov ecx, msg3          
   mov edx, len3         
   int 0x80

   ; moving the first number to eax register and second number to ebx
   ; and subtracting ascii '0' to convert it into a decimal number
	
   mov eax, [num1]
   sub eax, '0'
	
   mov ebx, [num2]
   sub ebx, '0'

   ; add eax and ebx
   add eax, ebx
   ; add '0' to to convert the sum from decimal to ASCII
   add eax, '0'

   ; storing the sum in memory location res
   mov [res], eax

   ; print the sum 
   mov eax, SYS_WRITE        
   mov ebx, STDOUT
   mov ecx, res         
   mov edx, 1        
   int 0x80

exit:    
   
   mov eax, SYS_EXIT   
   xor ebx, ebx 
   int 0x80
;SUBTRACTION
;test here: https://www.jdoodle.com/compile-assembler-nasm-online

section .text
global _start 

_start:           ;entry point
sub ah, ah
mov al, '5'
sub al, '2'
aas
or al, 30h
mov [res], ax

mov edx, len       ;message length
mov ecx, msg       ;message to write
mov ebx, 1         ;file descriptor (stdout)
mov eax, 4         ;system call number (sys_write)
int 0x80           ;call kernel

mov edx, 1         ;message length
mov ecx, res       ;message to write
mov ebx, 1         ;file descriptor (stdout)
mov eax, 4         ;system call number (sys_write)
int 0x80           ;call kernel

mov eax, 1         ;system call number (sys_exit)
int 0x80           ;call kernel

section .data
msg db 'The Result is:', 0xa
len equ $ - msg
section .bss
res resb 1

;;;output: 3


 

Screenshot

 

Notes

If you need an app to test your code, try emu8086

Addition, ran in emu8086

.MODEL SMALL
.STACK 200H
.DATA
NUM1 DB 12 ; number 1 to be added
NUM2 DB 10 ; number 2 to be added
VAL DW ?
MSG3 DB "Result: "
DECIMAL DB "00000$"

.CODE
BEGIN PROC
MOV AX, @DATA
MOV DS, AX

XOR AX, AX
MOV AL, NUM1
ADD AL, NUM2
ADC AH, 0
MOV VAL, AX

MOV AX, VAL
CALL AX_to_DEC

LEA DX, MSG3
MOV AH, 9
INT 21H

MOV AX, 4C00H
INT 21H
BEGIN ENDP

AX_to_DEC PROC
mov bx, 10 ; divisor
xor cx, cx ; CX=0 (number of digits)

First_Loop:
xor dx, dx ; Attention: DIV applies also DX!
div bx ; DX:AX / BX = AX remainder: DX
push dx ; LIFO
inc cx ; increment number of digits
test ax, ax ; AX = 0?
jnz First_Loop ; no: once more

mov di, OFFSET DECIMAL ; target string DECIMAL
Second_Loop:
pop ax ; get back pushed digit
or ax, 00110000b ; to ASCII
mov byte ptr [di], al ; save AL
inc di ; DI points to next character in string DECIMAL
loop Second_Loop ; until there are no digits left

mov byte ptr [di], '


Information

DB – Define Byte (Size – 1 Byte)

DW – Define Word (Size – 2 Byte)

DD – Define Double word (Size - 4 Bytes)

DQ – Define Quad word (Size – 8 Bytes)

DT – Define Ten Bytes (Size – 10 Bytes)

https://pinetools.com/syntax-highlighter
 ; End-of-string delimiter for INT 21 / FN 09h
ret
AX_to_DEC ENDP

END BEGIN


Information

DB – Define Byte (Size – 1 Byte)

DW – Define Word (Size – 2 Byte)

DD – Define Double word (Size – 4 Bytes)

DQ – Define Quad word (Size – 8 Bytes)

DT – Define Ten Bytes (Size – 10 Bytes)

https://pinetools.com/syntax-highlighter