0% found this document useful (0 votes)
18 views46 pages

MP Code WTH Op

The document consists of six practical assignments focused on writing X86/64 assembly language programs. Each practical has a specific aim, such as accepting hexadecimal numbers, counting positive and negative numbers, and converting between hexadecimal and BCD. The source code for each practical is provided, demonstrating various assembly programming techniques and system calls.

Uploaded by

Zaid Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views46 pages

MP Code WTH Op

The document consists of six practical assignments focused on writing X86/64 assembly language programs. Each practical has a specific aim, such as accepting hexadecimal numbers, counting positive and negative numbers, and converting between hexadecimal and BCD. The source code for each practical is provided, demonstrating various assembly programming techniques and system calls.

Uploaded by

Zaid Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Practical No 1

Name: Gaikwad Pratiksha Vinayak

Aim: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store

them in an array and display the accepted numbers.

Source code:
section .data

msg1 db 10,13,"Enter 5 64 bit numbers"

len1 equ $-msg1

msg2 db 10,13,"Entered 5 64 bit numbers"

len2 equ $-msg2

section .bss

array resd 200

counter resb 1

section .text

global _start

_start:

;display

mov Rax,1

mov Rdi,1

mov Rsi,msg1

mov Rdx,len1

syscall

;accept

mov byte[counter],05

mov rbx,00

loop1:

mov rax,0 ; 0 for read

mov rdi,0 ; 0 for keyboard

mov rsi, array ;move pointer to start of array

add rsi,rbx

mov rdx,17

syscall
add rbx,17 ;to move counter

dec byte[counter]

JNZ loop1

;display

mov Rax,1

mov Rdi,1

mov Rsi,msg2

mov Rdx,len2

syscall

;display

mov byte[counter],05

mov rbx,00

loop2:

mov rax,1 ;1 for write

mov rdi, 1 ;1 for monitor

mov rsi, array

add rsi,rbx

mov rdx,17 ;16 bit +1 for enter

syscall

add rbx,17

dec byte[counter]

JNZ loop2

;exit system call

mov rax ,60

mov rdi,0

syscall
Output:
Practical No 2
Name: Gaikwad Pratiksha Vinayak

Aim: Write an X86/64 ALP to accept a string and to display its length.

Source Code:
section .data

msg1 db 10,13,"Enter a string:"

len1 equ $-msg1

section .bss

str1 resb 200 ;string declaration

result resb 16

section .text

global _start

_start:

;display

mov Rax,1

mov Rdi,1

mov Rsi,msg1

mov Rdx,len1

syscall

;store string

mov rax,0

mov rdi,0

mov rsi,str1

mov rdx,200

syscall

call display

;exit system call

mov Rax ,60

mov Rdi,0

syscall

%macro dispmsg 2
mov Rax,1

mov Rdi,1

mov rsi,%1

mov rdx,%2

syscall

%endmacro

display:

mov rbx,rax ; store no in rbx

mov rdi,result ;point rdi to result variable

mov cx,16 ;load count of rotation in cl

up1:

rol rbx,04 ;rotate no of left by four bits

mov al,bl ; move lower byte in al

and al,0fh ;get only LSB

cmp al,09h ;compare with 39h

jg add_37 ;if greater than 39h skip add 37

add al,30h

jmp skip ;else add 30

add_37:

add al,37h

skip:

mov [rdi],al ;store ascii code in result variable

inc rdi ; point to next byte

dec cx ; decrement counter

jnz up1 ; if not zero jump to repeat

dispmsg result,16 ;call to macro

ret
Output:
Practical No 3
Name: Gaikwad Pratiksha Vinayak

Aim: Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.

Source Code:
section .data

array db 11h, 55h, 33h, 22h,44h

msg1 db 10,13,"Largest no in an array is:"

len1 equ $-msg1

section .bss

cnt resb 1

result resb 16

%macro dispmsg 2

mov Rax,1

mov Rdi,1

mov rsi,%1

mov rdx,%2

syscall

%endmacro

section .text

global _start

_start:

mov byte[cnt],5

mov rsi,array

mov al,0

LP: cmp al,[rsi]

jg skip

xchg al ,[rsi]

skip: inc rsi

dec byte[cnt]

jnz LP

;display al
call display

;display message

mov Rax,1

mov Rdi,1

mov Rsi,msg1

mov Rdx,len1

syscall

dispmsg result,16 ;call to macro

;exit system call

mov Rax ,60

mov Rdi,0

syscall

display:

mov rbx,rax ; store no in rbx

mov rdi,result ;point rdi to result variable

mov cx,16 ;load count of rotation in cl

up1:

rol rbx,04 ;rotate no of left by four bits

mov al,bl ; move lower byte in dl

and al,0fh ;get only LSB

cmp al,09h ;compare with 39h

jg add_37 ;if greater than 39h skip add 37

add al,30h

jmp skip1 ;else add 30

add_37:

add al,37h

skip1:

mov [rdi],al ;store ascii code in result variable

inc rdi ; point to next byte

dec cx ; decrement counter

jnz up1 ; if not zero jump to repeat

ret
Output:
Practical No 4
Name: Gaikwad Pratiksha Vinayak

Aim: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic

operations (+,-,*, /) using suitable macros. Define procedure for each operation.

Source Code:
section .data

menumsg db 10, '****** Menu ******', 10

db '1: Addition', 10

db '2: Subtraction', 10

db '3: Multiplication', 10

db '4: Division', 10

db 10, 'Enter your choice: '

menumsg_len equ $-menumsg

addmsg db 10, 'Welcome to Addition', 10

addmsg_len equ $-addmsg

submsg db 10, 'Welcome to Subtraction', 10

submsg_len equ $-submsg

mulmsg db 10, 'Welcome to Multiplication', 10

mulmsg_len equ $-mulmsg

divmsg db 10, 'Welcome to Division', 10

divmsg_len equ $-divmsg

wrchmsg db 10, 'You Entered a Wrong Choice...', 10

wrchmsg_len equ $-wrchmsg

resmsg db 10, 'Result is: '

resmsg_len equ $-resmsg

no1 dq 8h

no2 dq 2h

result dq 0

nwmsg db 10

section .bss

choice resb 1
dispbuff resb 16

%macro scall 4

mov rax, %1

mov rdi, %2

mov rsi, %3

mov rdx, %4

syscall

%endmacro

section .text

global _start

_start:

main_loop:

scall 1, 1, menumsg, menumsg_len ; Display menu

scall 0, 0, choice, 1 ; Read user input

cmp byte [choice], '1'

je add_proc

cmp byte [choice], '2'

je sub_proc

cmp byte [choice], '3'

je mul_proc

cmp byte [choice], '4'

je div_proc

; Invalid input case

scall 1, 1, wrchmsg, wrchmsg_len

jmp main_loop

add_proc:

mov rax, [no1]

add rax, [no2]

mov [result], rax

scall 1, 1, addmsg, addmsg_len

scall 1, 1, resmsg, resmsg_len

mov rbx, [result]

call disp64num
jmp main_loop

sub_proc:

mov rax, [no1]

sub rax, [no2]

mov [result], rax

scall 1, 1, submsg, submsg_len

scall 1, 1, resmsg, resmsg_len

mov rbx, [result]

call disp64num

jmp main_loop

mul_proc:

mov rax, [no1]

mov rbx, [no2]

mul rbx

mov [result], rax

scall 1, 1, mulmsg, mulmsg_len

scall 1, 1, resmsg, resmsg_len

mov rbx, [result]

call disp64num

jmp main_loop

div_proc:

xor rdx, rdx ; Clear remainder

mov rax, [no1]

mov rbx, [no2]

div rbx

mov [result], rax

scall 1, 1, divmsg, divmsg_len

scall 1, 1, resmsg, resmsg_len

mov rbx, [result]

call disp64num

jmp main_loop

disp64num:

mov rcx, 16
lea rdi, [dispbuff]

disp_loop:

rol rbx, 4

mov al, bl

and al, 0x0F

cmp al, 9

jbe digit

add al, 'A' - 10

jmp write

digit:

add al, '0'

write:

mov [rdi], al

inc rdi

loop disp_loop

scall 1, 1, dispbuff, 16

ret
Output:
Practical No 5
Name: Gaikwad Pratiksha Vinayak

Aim: Write an X86/64 ALP to count number of positive and negative numbers from the array.

Source Code:
section .data

welmsg db 10, 'Welcome to count positive and negative numbers in an array', 10

welmsg_len equ $-welmsg

pmsg db 10, 'Count of +ve numbers: '

pmsg_len equ $-pmsg

nmsg db 10, 'Count of -ve numbers: '

nmsg_len equ $-nmsg

nwline db 10

array dw 8505h, 90ffh, 0087h, 0088h, 8a9fh, 00adh, 0002h, 8507h

arrcnt equ 8

pcnt db 0

ncnt db 0

section .bss

dispbuff resb 2

%macro print 2

mov rax, 1 ; sys_write

mov rdi, 1 ; stdout

mov rsi, %1 ; message address

mov rdx, %2 ; message length

syscall

%endmacro

section .text

global _start

_start:

print welmsg, welmsg_len ; Print welcome message


mov esi, array ; Point to the start of the array

mov ecx, arrcnt ; Load array count into ecx

up1:

bt word [esi], 15 ; Test the sign bit (15th bit)

jnc pnxt ; If not set, jump to positive logic

inc byte [ncnt] ; Increment negative count

jmp pskip ; Skip to end of loop

pnxt:

inc byte [pcnt] ; Increment positive count

pskip:

add esi, 2 ; Move to next word (16-bit)

loop up1 ; Decrement ecx and repeat until 0

; Print positive count

print pmsg, pmsg_len

mov bl, [pcnt] ; Load positive count

call disp8num

; Print negative count

print nmsg, nmsg_len

mov bl, [ncnt] ; Load negative count

call disp8num

print nwline, 1 ; Print newline

; Exit the program

mov rax, 60 ; sys_exit

xor rdi, rdi ; Exit status 0

syscall

disp8num:

mov ecx, 2 ; Number of digits to display

mov edi, dispbuff ; Point to display buffer

dup1:

rol bl, 4 ; Rotate to get MS digit to LS digit

mov al, bl ; Move rotated value to al

and al, 0fh ; Mask upper bits

cmp al, 9
jbe dskip

add al, 07h ; Handle A-F digits

dskip:

add al, 30h ; Convert to ASCII

mov [edi], al ; Store in buffer

inc edi ; Increment buffer pointer

loop dup1

print dispbuff, 2 ; Print the buffer

ret

Output:
Practical No 6
Name: Gaikwad Pratiksha Vinayak

Aim: Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number

and 5- digit BCD number into its equivalent HEX number. Make your program user friendly to

accept the choice from user for: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper

strings to prompt the user while accepting the input and displaying the

result. (Wherever necessary, use 64-bit registers).

Source Code:
section .data

msg1 db 10,10, '###### Menu for Code Conversion ######', 10

db '1: Hex to BCD', 10

db '2: BCD to Hex', 10

db '3: Exit', 10,10, 'Enter Choice: '

msg1len equ $-msg1

msg2 db 10,10, 'Enter 4-digit hex number: ', 10

msg2len equ $-msg2

msg3 db 10,10, 'BCD Equivalent: ', 10

msg3len equ $-msg3

msg4 db 10,10, 'Enter 5-digit BCD number: ', 10

msg4len equ $-msg4

msg5 db 10,10, 'Invalid Choice! Please try again...', 10,10

msg5len equ $-msg5

msg6 db 10,10, 'Hex Equivalent: ', 10

msg6len equ $-msg6

newline db 10, 10

section .bss

arr resb 6 ; Buffer for user input

dispbuff resb 16 ; Buffer for output display

section .text
global _start

_start:

menu:

; Display the menu

mov rax, 1 ; syscall: write

mov rdi, 1 ; file descriptor: stdout

mov rsi, msg1 ; address of menu

mov rdx, msg1len ; length of menu

syscall

; Accept user choice

mov rax, 0 ; syscall: read

mov rdi, 0 ; file descriptor: stdin

mov rsi, arr ; buffer for input

mov rdx, 2 ; max input length (1 char + newline)

syscall

cmp byte [arr], '1' ; Check if choice is 1

je hex_to_bcd

cmp byte [arr], '2' ; Check if choice is 2

je bcd_to_hex

cmp byte [arr], '3' ; Check if choice is 3

je exit_program

; Invalid choice

mov rax, 1

mov rdi, 1

mov rsi, msg5 ; address of invalid choice message

mov rdx, msg5len ; length of message

syscall

jmp menu

hex_to_bcd:

; Prompt for hex input

mov rax, 1

mov rdi, 1

mov rsi, msg2 ; address of hex input prompt


mov rdx, msg2len ; length of prompt

syscall

; Accept hex input (4 digits + newline)

mov rax, 0

mov rdi, 0

mov rsi, arr

mov rdx, 5

syscall

; Convert hex to BCD

xor rbx, rbx ; Clear rbx

mov rcx, 4 ; Hex digit count

mov rsi, arr ; Point to hex input

convert_hex_to_bcd:

rol rbx, 4 ; Rotate left 4 bits

mov al, [rsi]

cmp al, '9'

jbe hex_digit

sub al, 7 ; Adjust for A-F

hex_digit:

sub al, '0' ; Convert to binary

add bl, al

inc rsi

loop convert_hex_to_bcd

; Output BCD equivalent

mov rax, 1

mov rdi, 1

mov rsi, msg3 ; address of BCD output message

mov rdx, msg3len ; length of message

syscall

; Convert binary to ASCII

mov rcx, 0 ; Counter for digits

mov ax, bx ; Load binary value

mov bx, 10 ; Decimal base


convert_binary_to_ascii:

xor dx, dx ; Clear dx

div bx ; Divide ax by 10

push dx ; Push remainder onto stack

inc rcx

test ax, ax

jnz convert_binary_to_ascii

; Display ASCII digits

display_bcd:

pop dx ; Pop remainder

add dl, '0' ; Convert to ASCII

mov [dispbuff], dl

mov rax, 1

mov rdi, 1

mov rsi, dispbuff ; Output ASCII digit

mov rdx, 1

syscall

loop display_bcd

jmp menu

bcd_to_hex:

; Prompt for BCD input

mov rax, 1

mov rdi, 1

mov rsi, msg4 ; address of BCD input prompt

mov rdx, msg4len ; length of prompt

syscall

; Accept BCD input (5 digits + newline)

mov rax, 0

mov rdi, 0

mov rsi, arr

mov rdx, 6

syscall

; Convert BCD to Hex


xor rax, rax ; Clear rax

xor rbx, rbx ; Clear rbx

mov rcx, 5 ; Digit count

mov rsi, arr ; Point to BCD input

convert_bcd_to_hex:

mul bx ; Multiply current result by base (10)

mov bl, [rsi]

sub bl, '0' ; Convert ASCII to binary

add rax, rbx ; Add to result

inc rsi

loop convert_bcd_to_hex

; Output Hex equivalent

mov rax, 1

mov rdi, 1

mov rsi, msg6 ; address of hex output message

mov rdx, msg6len ; length of message

syscall

; Convert binary to hex ASCII

mov rcx, 4 ; Hex digit count

mov rbx, rax ; Load binary result

convert_hex_to_ascii:

rol rbx, 4 ; Rotate left 4 bits

mov dl, bl

and dl, 0x0F ; Mask lowest nibble

add dl, '0'

cmp dl, '9'

jbe hex_output_digit

add dl, 7 ; Adjust for A-F

hex_output_digit:

mov [dispbuff], dl

mov rax, 1

mov rdi, 1

mov rsi, dispbuff ; Output ASCII hex digit


mov rdx, 1

syscall

loop convert_hex_to_ascii

jmp menu

exit_program:

mov rax, 60 ; syscall: exit

xor rdi, rdi ; exit code: 0

syscall

Output:
Practical No 7
Name: Gaikwad Pratiksha Vinayak

Aim: Write X86/64 ALP to detect protected mode and display the values of GDTR, LDTR,

IDTR, TR and MSW Registers also identify CPU type using CPUID instruction.

Source Code:
section .data

rmodemsg db 10, 'Processor is in Real Mode', 10

rmsg_len equ $-rmodemsg

pmodemsg db 10, 'Processor is in Protected Mode', 10

pmsg_len equ $-pmodemsg

gdtmsg db 10, 'GDT Contents:', 10

gmsg_len equ $-gdtmsg

ldtmsg db 10, 'LDT Contents:', 10

lmsg_len equ $-ldtmsg

idtmsg db 10, 'IDT Contents:', 10

imsg_len equ $-idtmsg

trmsg db 10, 'Task Register Contents:', 10

tmsg_len equ $-trmsg

mswmsg db 10, 'Machine Status Word:', 10

mmsg_len equ $-mswmsg

colonmsg db ':', 10

colmsg_len equ $-colonmsg

newline db 10

nwline_len equ $-newline

section .bss

gdt resb 10 ; Reserve space for GDT (base + limit)

ldt resb 2 ; Reserve space for LDT

idt resb 10 ; Reserve space for IDT (base + limit)

tr resb 2 ; Reserve space for Task Register

cr0_data resb 4 ; Reserve space for CR0


disp_buff resb 8 ; Buffer for displaying hex values

section .text

global _start

_start:

; Check Processor Mode

mov eax, cr0_data

smsw eax ; Read Machine Status Word (CR0)

mov [cr0_data], eax

bt eax, 1 ; Check Protection Enable bit (PE)

jc protected_mode

call real_mode

protected_mode:

mov rsi, pmodemsg

mov rdx, pmsg_len

call display_message

jmp read_system_tables

real_mode:

mov rsi, rmodemsg

mov rdx, rmsg_len

call display_message

read_system_tables:

; Read and Display System Tables

sgdt [gdt] ; Load GDT

sldt [ldt] ; Load LDT

sidt [idt] ; Load IDT

str ax ; Store Task Register

mov [tr], ax

; Display GDT

mov rsi, gdtmsg

mov rdx, gmsg_len

call display_message

call display_gdt

; Display LDT
mov rsi, ldtmsg

mov rdx, lmsg_len

call display_message

call display_ldt

; Display IDT

mov rsi, idtmsg

mov rdx, imsg_len

call display_message

call display_idt

; Display Task Register

mov rsi, trmsg

mov rdx, tmsg_len

call display_message

call display_tr

; Display Machine Status Word (CR0)

mov rsi, mswmsg

mov rdx, mmsg_len

call display_message

call display_cr0

; Exit

mov rax, 60 ; syscall: exit

xor rdi, rdi ; exit code: 0

syscall

display_message:

mov rax, 1 ; syscall: write

mov rdi, 1 ; file descriptor: stdout

syscall

ret

display_gdt:

mov rbx, [gdt + 4] ; Upper 4 bytes of GDT base address

call display_number

mov rbx, [gdt + 2] ; Limit of GDT

call display_number
mov rbx, [gdt] ; Lower 2 bytes of GDT base address

call display_number

ret

display_ldt:

mov rbx, [ldt] ; LDT register value

call display_number

ret

display_idt:

mov rbx, [idt + 4] ; Upper 4 bytes of IDT base address

call display_number

mov rbx, [idt + 2] ; Limit of IDT

call display_number

mov rbx, [idt] ; Lower 2 bytes of IDT base address

call display_number

ret

display_tr:

mov rbx, [tr] ; Task Register value

call display_number

ret

display_cr0:

mov rbx, [cr0_data + 2] ; Higher 2 bytes of CR0

call display_number

mov rbx, [cr0_data] ; Lower 2 bytes of CR0

call display_number

ret

display_number:

mov rcx, 8 ; Number of digits to display

mov rsi, disp_buff ; Buffer for storing ASCII values

convert_to_ascii:

rol rbx, 4 ; Rotate left by 4 bits

mov al, bl ; Get lower byte

and al, 0x0F ; Mask upper bits

cmp al, 9
jbe digit

add al, 7 ; Adjust for hex letters (A-F)

digit:

add al, 48 ; Convert to ASCII

mov [rsi], al

inc rsi

loop convert_to_ascii

; Display the buffer

mov rax, 1 ; syscall: write

mov rdi, 1 ; file descriptor: stdout

mov rsi, disp_buff

mov rdx, 8

syscall

ret
Output:
Practical No 8
Name: Gaikwad Pratiksha Vinayak

Aim: Write X86/64 ALP to perform non-overlapped block transfer without string specific

instructions. Block containing data can be defined in the data segment.

Source Code:
global _start

_start:

%macro disp 2

mov rax,1

mov rdi,1

mov rsi,%1

mov rdx,%2

syscall

%endmacro

%macro accept 2

mov rax,0

mov rdi,0

mov rsi,%1

mov rdx,%2

syscall

%endmacro

section .text

menu:

disp msg2,len2

disp msg,len

accept choice,02

mov al,byte[choice]

cmp al,31h

je without_string

cmp al,32h

je with_string
cmp al,33h

je exit

without_string:

mov rsi,sarr

mov rdi,darr

a1:

mov al,[rsi]

mov [rdi],al

inc rsi

inc rdi

dec byte[cnt]

jnz a1

disp msg1,len1

mov rsi,darr

a2:

mov rax,[rsi]

push rsi

call displayproc

pop rsi

inc rsi

dec byte[cnt1]

jnz a2

jmp menu

with_string:

mov rsi,sarr

mov rdi,darr

CLD

mov rcx,05

rep movsb

disp msg1,len1

mov rsi,darr

a3:

mov rax,[rsi]
push rsi

call displayproc

pop rsi

inc rsi

dec byte[cnt2]

jnz a3

jmp menu

exit:

mov rax,60

mov rdi,0

syscall

displayproc: ;display procedure for displaying darr

mov rsi,disparr+1

mov rcx,2

l4:mov rdx,0

mov rbx,10h

div rbx

cmp dl,09h

jle l5

add dl,07h

l5:add dl,30h

mov [rsi],dl

dec rsi

dec rcx

jnz l4

mov rax,1

mov rdi,1

mov rsi,disparr

mov rdx,2

syscall

ret

section .data

sarr db 01H,02H,03H,04H,05H
darr db 00H,00H,00H,00H,00H

cnt db 05

cnt1 db 05

cnt2 db 05

msg db "Menu",10

db "1.Without_string",10

db "2.with_string",10

db "3.Exit",10

db 10, "Enter choice",10

len equ $-msg

msg1 db 10,"Elements copied in destination array:"

len1 equ $-msg1

msg2 db " ",10

len2 equ $-msg2

section .bss

disparr resb 02

choice resb 02

%ifdef COMMENT

Menu

1.Without_string

2.with_string

3.Exit

Enter choice

Elements copied in destination array:0102030405

Menu

1.Without_string

2.with_string

3.Exit

Enter choice

Elements copied in destination array:0102030405

Menu
1.Without_string

2.with_string

3.Exit

Enter choice

%endif

Output:
Practical No 9
Name: Gaikwad Pratiksha Vinayak

Aim: Write X86/64 ALP to perform overlapped block transfer with string specific instructions

Block containing data can be defined in the data segment.

Source Code:
global _start

_start:

%macro disp 2

mov rax,1

mov rdi,1

mov rsi,%1

mov rdx,%2

syscall

%endmacro

%macro accept 2

mov rax,0

mov rdi,0

mov rsi,%1

mov rdx,%2

syscall

%endmacro

section .text

menu:

disp msg2,len2

disp msg,len

accept choice,02

mov al,byte[choice]

cmp al,31h

je without_string

cmp al,32h

je with_string
cmp al,33h

je exit

without_string:

mov rsi,arr1

mov rdi,arr1

add rsi,09

add rdi,13

a1:

mov al,[rsi]

mov [rdi],al

dec rsi

dec rdi

dec byte[cnt1]

jnz a1

disp msg1,len1

mov rsi,arr1

a2:

mov rax,[rsi]

push rsi

call displayproc

pop rsi

inc rsi

dec byte[cnt2]

jnz a2

jmp menu

with_string:

mov rsi,arr2

mov rdi,arr2

add rsi,09

add rdi,13

STD

mov rcx,10

rep movsb
disp msg1,len1

mov rsi,arr2

a3:

mov rax,[rsi]

push rsi

call displayproc

pop rsi

inc rsi

dec byte[cnt3]

jnz a3

jmp menu

exit:

mov rax,60

mov rdi,0

syscall

displayproc: ;display procedure for displaying arr

mov rsi,disparr+1

mov rcx,2

l4:mov rdx,0

mov rbx,10h

div rbx

cmp dl,09h

jle l5

add dl,07h

l5:add dl,30h

mov [rsi],dl

dec rsi

dec rcx

jnz l4

mov rax,1

mov rdi,1

mov rsi,disparr

mov rdx,2
syscall

ret

section .data

arr1 db 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH

arr2 db 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH

cnt1 db 10

cnt2 db 15

cnt3 db 15

msg db "Menu",10

db "1.Without_string",10

db "2.with_string",10

db "3.Exit",10

db "Enter choice",10

len equ $-msg

msg1 db 10,"Elements copied in array:",10

len1 equ $-msg1

msg2 db " ",10

len2 equ $-msg2

section .bss

disparr resb 02

choice resb 02

%ifdef COMMENT

Menu

1.Without_string

2.with_string

3.Exit

Enter choice

Elements copied in array:

010203040102030405060708090A0F

Menu

1.Without_string

2.with_string
3.Exit

Enter choice

Elements copied in array:

010203040102030405060708090A0F

Menu

1.Without_string

2.with_string

3.Exit

Enter choice

%endif
Output:
Practical No 10
Name: Gaikwad Pratiksha Vinayak

Aim: 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).

Source Code:
section .data

msg1 db "Multiplication using Successive Addition and Add-Shift Method", 10, 0

msg2 db "Enter first 8-bit hex number: ", 0

msg3 db "Enter second 8-bit hex number: ", 0

msg4 db "Result (Successive Addition): 0x", 0

msg5 db "Result (Add-Shift Method): 0x", 0

newline db 10, 0

section .bss

num1 resb 1 ; First number (8-bit)

num2 resb 1 ; Second number (8-bit)

result1 resd 1 ; Result from Successive Addition (32-bit)

result2 resd 1 ; Result from Add-Shift Method (32-bit)

buffer resb 9 ; Buffer for hexadecimal output (8 chars + null terminator)

section .text

global _start

_start:

; Display welcome message

mov rsi, msg1 ; Load message 1

call print_string

; Prompt and read the first number

mov rsi, msg2 ; Load prompt message

call print_string

call read_hex ; Read hex number

mov [num1], al ; Store first number

; Prompt and read the second number

mov rsi, msg3 ; Load prompt message


call print_string

call read_hex ; Read hex number

mov [num2], al ; Store second number

; --- Successive Addition Method ---

xor rax, rax ; Clear RAX

xor rcx, rcx ; Clear RCX (Result Accumulator)

mov al, [num1] ; Load the first number into AL

mov bl, [num2] ; Load the second number into BL

successive_addition:

add rcx, rax ; Add AL (num1) to RCX (result accumulator)

dec bl ; Decrement BL (num2)

jnz successive_addition ; Repeat until BL == 0

mov [result1], ecx ; Store the result into result1

; Display Successive Addition Result

mov rsi, msg4 ; Load message for Successive Addition Result

call print_string

mov eax, [result1] ; Load the result

call print_hex ; Print it in hexadecimal

call print_newline ; Print newline

; --- Add-Shift Method ---

xor rax, rax ; Clear RAX

xor rcx, rcx ; Clear RCX (Result Accumulator)

mov al, [num1] ; Load the first number into AL

mov bl, [num2] ; Load the second number into BL

add_shift_method:

test bl, 1 ; Check if least significant bit of BL is set

jz shift_left ; If not, skip addition

add rcx, rax ; Add AL (num1) to RCX

shift_left:

shl rax, 1 ; Left-shift num1 (multiply by 2)

shr bl, 1 ; Right-shift num2 (divide by 2)

jnz add_shift_method ; Repeat until BL == 0

mov [result2], rcx ; Store the result into result2


; Display Add-Shift Result

mov rsi, msg5 ; Load message for Add-Shift Result

call print_string

mov eax, [result2] ; Load the result

call print_hex ; Print it in hexadecimal

call print_newline ; Print newline

; Exit the program

mov rax, 60 ; System call for exit

xor rdi, rdi ; Exit code 0

syscall

; --- Helper Functions ---

; Print null-terminated string

print_string:

mov rax, 1 ; System call for write

mov rdi, 1 ; File descriptor (stdout)

mov rdx, 256 ; Max string length

syscall

ret

; Print newline

print_newline:

mov rsi, newline ; Pointer to newline string

call print_string

ret

; Read an 8-bit hex number

read_hex:

mov rax, 0 ; System call for read

mov rdi, 0 ; File descriptor (stdin)

lea rsi, buffer ; Temporary buffer

mov rdx, 2 ; Read 2 characters

syscall

sub byte [buffer], '0' ; Convert ASCII '0'–'9' to integer

mov al, byte [buffer] ; Move number into AL

ret
; Print a hexadecimal number (32-bit)

print_hex:

xor rbx, rbx ; Clear RBX (base)

mov bl, 16 ; Hexadecimal base

lea rsi, buffer + 8 ; Pointer to the end of buffer

mov byte [rsi], 0 ; Null-terminate the buffer

print_hex_loop:

xor rdx, rdx ; Clear remainder

div rbx ; Divide EAX by 16

add dl, '0' ; Convert remainder to ASCII

cmp dl, '9'

jbe store_digit

add dl, 7 ; Convert 'A'–'F'

store_digit:

dec rsi ; Move buffer pointer backward

mov [rsi], dl ; Store character

test eax, eax ; Check if quotient is zero

jnz print_hex_loop

mov rsi, rsi ; Adjust to start of the buffer

call print_string ; Print the buffer

ret
Output:

You might also like