Assignment No. 1

Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers stored in the
memory. Accept input from the user.

;MIL Assignment No. 1
;X86/64 Assembly language program (ALP)
;Addition of N 16-bit nos, result is 32 bit.
;Accept input from the user. Display the result.

;macros as per 64-bit convensions
;—————————————————————————–
%macro  print   2
mov     eax,4        ; Function 1 – write
mov     ebx,1        ; To stdout
mov     ecx,%1        ; String address
mov     edx,%2        ; String size
int 80h            ; invoke operating system to WRITE
%endmacro print%macro  read   2
mov     eax,3        ; Function 0 – Read
mov     ebx,0        ; from stdin
mov     ecx,%1        ; buffer address
mov     edx,%2        ; buffer size
int 80h            ; invoke operating system to READ
%endmacro read

%macro    exit    0
mov eax, 1        ; system call 60 is exit
int 80h             ; invoke operating system to exit
%endmacro exit
;——————————————————————

;———————————————————————
section .data
nline        db    10,10
nline_len:    equ    $-nline

msg         db     10,13,”MIL assignment 01 : Addition of N 16-bit nos.”
db    10,13,”———————————————”
msg_len:    equ     $-msg

smsg         db     10,13,”Enter size of array <????> = ”
smsg_len:    equ     $-smsg

amsg         db     10,13,”Enter data <????>    = ”
amsg_len:    equ     $-amsg

dmsg         db     10,13,”Result <????????>    = ”
dmsg_len:    equ     $-dmsg

emsg         db     10,13,”You entered Invalid Data!!!”,10
emsg_len:    equ     $-emsg
;———————————————————————
section .bss
buf         resb     5        ;4digits + enter
buf_len:    equ     $-buf
size        resw    1
ansl        resw    1
ansh        resw    1
char_ans    resb    4
;———————————————————————
section .text
global _start
_start:
print    msg,msg_len    ; “MIL assignment 01 : Addition of N 16-bit nos.”

print    smsg,smsg_len    ; “Enter size of array <????> = ”
call    accept_16
mov    [size],bx

mov     ebp,[size]
next_num:
print    amsg,amsg_len    ; “Enter data <????>    = ”
call    accept_16
add    [ansl],bx
jnc    next

inc    word [ansh]
next:    dec    ebp
jnz    next_num

print     dmsg,dmsg_len    ; “Result <???? ????>    = ”
mov     eax,[ansh]    ; load value of ansh in rax
call     display_16    ; display result

mov     eax,[ansl]    ; load value of ansl in rax
call     display_16    ; display result

print    nline, nline_len
exit
;——————————————————————
accept_16:
read    buf,buf_len

xor     bx,bx
mov     ecx,04
mov     esi,buf
next_digit:
rol    bx,04
mov    al,[esi]
cmp        al,’0′         ; ‘0’ = 30h or 48d
jb         error          ; jump if below ‘0’ to error
cmp        al,’9′
jbe        sub30         ; subtract 30h if no is in the range ‘0’-‘9′

cmp        al,’A’        ; ‘A’ = 41h or 65d
jb         error           ; jump if below ‘A’ to error
cmp        al,’F’
jbe        sub37           ; subtract 37h if no is in the range ‘A’-‘F’

cmp        al,’a’          ; ‘a’ = 61h or 97d
jb         error           ; jump if below ‘a’ to error
cmp        al,’f’
jbe     sub57           ; subtract 57h if no is in the range ‘a’-‘f’

error:  print     emsg,emsg_len    ; “You entered Invalid Data!!!”
exit

sub57:    sub        al,20h          ; subtract 57h if no is in the range ‘a’-‘f’
sub37:  sub        al,07h          ; subtract 37h if no is in the range ‘a’-‘f’
sub30:  sub        al,30h          ; subtract 30h if no is in the range ‘0’-‘9’

add    bx,ax        ; prepare number
inc    esi        ; point to next digit
loop    next_digit
ret

;——————————————————————
display_16:
mov     esi,char_ans+3    ; load last byte address of char_ans in rsi
mov     ecx,4        ; number of digits

cnt:    mov     edx,0        ; make rdx=0 (as in div instruction rdx:rax/rbx)
mov     ebx,16        ; divisor=16 for hex
div     ebx
cmp     dl, 09h        ; check for remainder in RDX
jbe      add30
add      dl, 07h
add30:
add     dl,30h        ; calculate ASCII code
mov     [esi],dl    ; store it in buffer
dec     esi        ; point to one byte back

dec     ecx        ; decrement count
jnz     cnt        ; if not zero repeat

print char_ans,4    ; display result on screen
ret
;—————————————————————-
The steps to run the above program is as follows:

sai@sai-ThinkCentre-A70:~$ nasm -f elf p1.asm
sai@sai-ThinkCentre-A70:~$ ld -m elf_i386 -s -o p1 p1.o
sai@sai-ThinkCentre-A70:~$ ./p1

MIL assignment 01 : Addition of N 16-bit nos.
———————————————
Enter size of array <????> = 0003
Enter data <????>    = 0005
Enter data <????>    = 0002
Enter data <????>    = 0003
Result <????????>    = 0000000A
sai@sai-ThinkCentre-A70:~$

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *