You are on page 1of 9

Assignment-04

Name : Manasi bharati


Roll no. : 224085
PRN NO : 22320136
Class : Comp SY-D2
Subject : Computer Architecture and Organization

Title: Write 64-bit ALP to accept the numbers from user and perform addition of 2

numbers and display the result on screen.

1) Description of Instructions used in this code.

2) Code with comments.

3) Screen shot of output

Theory :

1) Description of Instructions used in this code.


1. Section Definitions:
 .data: This section is used for declaring data and constants.
 .bss: This section is used for declaring uninitialized data.
2. Data Definitions:
 db: Declares a byte (8 bits) of data.
 resq: Reserves a quadword (64 bits) of memory space.
 format: Stores the format string for integer output.
3. Global Declaration:
 global _start: Declares the entry point for the program.
4. Labels:
 _start: The program's entry point.
5. Instructions:
 mov: Moves data from one location to another.
 add: Adds two values together.
 xor: Performs a bitwise XOR operation.
 syscall: Triggers a system call to perform various actions (e.g., input, output, and
program termination).
 call: Calls a subroutine or function.
 ret: Returns from a subroutine.
6. User Input and Output:
 print_string: Outputs a null-terminated string to the console.
 print_int: Outputs an integer to the console.
 read_int: Reads an integer from the console.
 atoi: Converts a null-terminated string to an integer.
7. Loop and Conditionals:
 test: Performs a bitwise AND operation for setting flags.
 jz: Jumps if the zero flag is set (checks for the null terminator).
 jmp: Unconditional jump.
8. System Call Numbers:
 rax is used to specify the system call number, with 60 for exit, and 0 for read, and
1 for write.
9. Registers:
 Registers like rdi, rsi, rdx, and rax are used for passing arguments and values to
and from system calls.
10. ASCII Operations:
 Converting ASCII characters to integers by subtracting the ASCII value of '0'.
This program combines these instructions and concepts to perform the following
tasks:
 Prompt the user for two numbers.
 Read and convert the user's input to integers.
 Add the two numbers together.
 Display the result on the screen.
 Terminate the program.
The program is structured to be executed on a 64-bit Linux system using x86-64
assembly language and makes use of various subroutines to handle input, output,
and integer conversion.
2) Code with comments.

%macro scall 4

mov rax, %1

mov rdi, %2

mov rsi, %3

mov rdx, %4

syscall

%endmacro

section .data

m1 db "Enter how many numbers you want to add (enter in two digit) :", 10d,13d

l1 equ $-m1

m2 db "Enter a 2 digit number :", 10d,13d

l2 equ $-m2

m3 db "array contents are :",10d,13d

l3 equ $-m3

m4 db "The addition of entered numbers is :", 10d,13d

l4 equ $-m4

m5 db " ", 10d,13d

l5 equ $-m5

section .bss

num resb 20

array resb 200

char_ans resb 16
cnt resb 20

cnt1 resb 20

cnt2 resb 20

section .text

global _start

_start:

;---------accept array size-----------

scall 1,1,m1,l1

scall 0,0,num,3

call accept_proc

mov [cnt],bx

mov [cnt1],bx

mov [cnt2],bx

;-----accept array elements----------

mov rbp,array

up1:

scall 1,1,m2,l2

scall 0,0,num,3

call accept_proc

mov [rbp],bx

add rbp,2

dec byte[cnt]

jnz up1

;------display array elements------


scall 1,1,m3,l3

mov ax,[cnt1]

mov [cnt],ax

mov rbx,array

back1:

mov ax,[rbx]

call display_proc

scall 1,1,char_ans,3

scall 1,1,m5,l5

add rbx,2

dec byte[cnt]

jnz back1

;----------array addition---------

mov ax,[cnt2]

mov [cnt],ax

mov ax,00h

mov rbx,array

back2:

add ax,[rbx]

add rbx,2

dec byte[cnt]

jnz back2

call display_proc

scall 1,1,m4,l4

scall 1,1,char_ans,3
scall 1,1,m5,l5

;--------exit------------

mov rax, 60

mov rdi, 0

syscall

;--------accept procedure----------

accept_proc:

mov rsi,num

mov rax,0

mov rbx,0

mov rcx,2

back:

rol rbx,04

mov al,[rsi]

cmp al,39h

jbe next

sub al,07h

next:

sub al,30h

add bx,ax

inc rsi

dec rcx

jnz back

ret
;------------------Display Procedure-------------------

display_proc:

mov rbp,char_ans

mov rcx,2

up3:

rol al,04

mov dl,al

and dl,0Fh

cmp dl,09h

jbe next1

add dl,07h

next1:

add dl,30h

mov [rbp],dl

inc rbp

dec rcx

JNZ up3

ret

;--------------------------------------

You might also like