;Om Nandurkar
;Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use
;successive addition and add and shift method. (use of 64-bit registers is expected).


section .data

msg db 'Enter two digit Number::',0xa
msg_len equ $-msg
res db 10,'Multiplication of elements is::'
res_len equ $-res
choice db 'Enter your Choice:',0xa
       db'1.Successive Addition',0xa
       db '2.Add and Shift method',0xa
       db '3.Exit',0xa
choice_len equ $-choice

section .bss
num resb 03
num1 resb 01
result resb 04
cho resb 2


section .text

global _start
_start:

 xor rax,rax
 xor rbx,rbx
 xor rcx,rcx
 xor rdx,rdx
 mov byte[result],0
 mov byte[num],0
 mov byte[num1],0
       
        mov rax,1                   
 mov rdi,1
 mov rsi,choice
 mov rdx,choice_len
 syscall

  
       
        mov rax,0                   ;; read choice
 mov rdi,0
 mov rsi,cho
 mov rdx,2
 syscall

 

 cmp byte[cho],31h           ;; comparing choice
 je a

 cmp byte[cho],32h
 je b
 
        jmp exit

 a:  call Succe_addition

 jmp _start

 b:  call Add_shift

 jmp _start

exit:
 mov rax,60
 mov rdi,0
 syscall

convert:                                          ;; ASCII to Hex conversion
 xor rbx,rbx
 xor rcx,rcx
 xor rax,rax

 mov rcx,02
 mov rsi,num
 up1:
 rol bl,04

 mov al,[rsi]
 cmp al,39h
 jbe p1
 sub al,07h
 jmp p2
 p1: sub al,30h
 p2: add bl,al
 inc rsi
 loop up1
ret

display:                       ;; Hex to ASCII conversion
 mov rcx,4
 mov rdi,result
 dup1:
 rol bx,4
 mov al,bl
 and al,0fh
 cmp al,09h
 jbe p3
 add al,07h
 jmp p4
 p3: add al,30h
 p4:mov [rdi],al
 inc rdi
 loop dup1
       
        mov rax,1
 mov rdi,1
 mov rsi,result
 mov rdx,4
 syscall

 
ret


Succe_addition:

        mov rax,1
 mov rdi,1
 mov rsi,msg                                      ;print for num1
 mov rdx,msg_len
 syscall

 
       
        mov rax,0
 mov rdi,0
 mov rsi,num                                      ;Read num 1
 mov rdx,3
 syscall

 

 call convert                                     ;convert to hex from ascii
 mov [num1],bl
 
        mov rax,1
 mov rdi,1
 mov rsi,msg
 mov rdx,msg_len                                  ;msg for 2nd number
 syscall

 
       
        mov rax,0
 mov rdi,0                                          ;Read 2nd number
 mov rsi,num
 mov rdx,3
 syscall


 call convert                                       ;convert 2nd number to hex
 xor rcx,rcx
 xor rax,rax
 mov rax,[num1]
 
 repet:
 add rcx,rax
 dec bl
 jnz repet

 mov [result],rcx

        mov rax,1
 mov rdi,1
 mov rsi,res
 mov rdx,res_len
 syscall

 

 mov rbx,[result]

 call display
ret



Add_shift:
  
        mov rax,1
 mov rdi,1
 mov rsi,msg
 mov rdx,msg_len
 syscall

 
       
        mov rax,0
 mov rdi,0
 mov rsi,num
 mov rdx,3
 syscall

 

 call convert
 mov [num1],bl
 
       
        mov rax,1
 mov rdi,1
 mov rsi,msg
 mov rdx,msg_len
 syscall

 
       
        mov rax,0
 mov rdi,0
 mov rsi,num
 mov rdx,3
 syscall

 

 call convert

 mov [num],bl

 xor rbx,rbx
 xor rcx,rcx
 xor rdx,rdx
 xor rax,rax
 mov dl,08
 mov al,[num1]
 mov bl,[num]

 p11:
        shr bx,01
 jnc p
 add cx,ax
 p:
        shl ax,01
 dec dl
 jnz p11

 mov [result],rcx

       
        mov rax,1
 mov rdi,1
 mov rsi,res
 mov rdx,res_len
 syscall

 ;dispmsg res,res_len

 mov rbx,[result]
 call display

ret

 

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