0% found this document useful (0 votes)
12 views

MP 4-7 (1)

The document contains several practical exercises in assembly language programming, focusing on hexadecimal arithmetic operations, counting positive and negative numbers, and converting between hexadecimal and BCD. Each practical includes macros for system calls, procedures for arithmetic operations, and handling user input. The exercises demonstrate various programming concepts in x86/64 assembly language.

Uploaded by

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

MP 4-7 (1)

The document contains several practical exercises in assembly language programming, focusing on hexadecimal arithmetic operations, counting positive and negative numbers, and converting between hexadecimal and BCD. Each practical includes macros for system calls, procedures for arithmetic operations, and handling user input. The exercises demonstrate various programming concepts in x86/64 assembly language.

Uploaded by

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

Practical No-04

%macro IO 4

mov rax,%1

mov rdi,%2

mov rsi,%3

mov rdx,%4

syscall

%endmacro

section .data

m1 db "enter choice (+,-,*, /)" ,10 ; 10d -> line feed

l1 equ $-m1

m2 db "Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic
operations (+,-,*, /) using suitable macros. Define procedure for each operation." ,10

l2 equ $-m2

m3 db "PRACTICAL 4" ,10

l3 equ $-m3

madd db "addition here" ,10

l4 equ $-madd

msub db "subtraction here" ,10

l5 equ $-msub

mmul db "multiplication here" ,10

l6 equ $-mmul

mdiv db "division here" ,10

l7 equ $-mdiv

mspace db 10

m_result db "result is "

m_result_l equ $-m_result

m_qou db "qoutient is "

m_qou_l equ $-m_qou

m_rem db "remainder is "

m_rem_l equ $-m_rem


m_default db "enter correct choice",10

m_default_l equ $-m_default

section .bss

choice resb 2

_output resq 1

_n1 resq 1

_n2 resq 1

temp_1 resq 1

temp_2 resq 1

section .text

global _start

_start:

IO 1,1,m2,l2

IO 1,1,m3,l3

IO 1,1,m1,l1

IO 0,0,choice,2

cmp byte [choice],'+'

jne case2

call add_fun

jmp exit

case2:

cmp byte [choice],'-'

jne case3

call sub_fun

jmp exit

case3:

cmp byte [choice],'*'

jne case4

call mul_fun

jmp exit

case4:
cmp byte [choice],'/'

jne case5

call div_fun

jmp exit

case5:

cmp byte [choice],'a'

jne error

call add_fun

call sub_fun

call mul_fun

call div_fun

jmp exit

error:

IO 1,1,m_default,m_default_l

jmp exit

exit:

mov rax, 60

mov rdi, 0

syscall

add_fun:

IO 1,1,madd,l4

mov qword[_output],0

IO 0,0,_n1,17

IO 1,1,_n1,17

call ascii_to_hex

add qword[_output],rbx

IO 0,0,_n1,17

IO 1,1,_n1,17

call ascii_to_hex

add qword[_output],rbx

mov rbx,[_output]
IO 1,1,mspace,1

IO 1,1,m_result,m_result_l

call hex_to_ascii

ret

sub_fun:

IO 1,1,msub,l5

mov qword[_output],0

IO 0,0,_n1,17

IO 1,1,_n1,17

;IO 1,1,mspace,1

call ascii_to_hex

add qword[_output],rbx

IO 0,0,_n1,17

IO 1,1,_n1,17

;IO 1,1,mspace,1

call ascii_to_hex

sub qword[_output],rbx

mov rbx,[_output]

IO 1,1,mspace,1

IO 1,1,m_result,m_result_l

call hex_to_ascii

ret

mul_fun:

IO 1,1,mmul,l6 ; message

IO 0,0,_n1,17 ; n1 input

IO 1,1,_n1,17

call ascii_to_hex; conversion returns hex value in rbx

mov [temp_1],rbx ; storing hex in temp_1

IO 0,0,_n1,17 ;n2 input

IO 1,1,_n1,17
call ascii_to_hex

mov [temp_2],rbx ; putting hex of n2 in temp_2

mov rax,[temp_1] ; temp_1->rax

mov rbx,[temp_2] ;temp_2->rbx

mul rbx ; multiplication

push rax

push rdx

IO 1,1,mspace,1

IO 1,1,m_result,m_result_l

pop rdx

mov rbx,rdx; setting rbx value for conversion

call hex_to_ascii

pop rax

mov rbx,rax; setting rbx value for conversion

call hex_to_ascii ; final output

ret

div_fun:

IO 1,1,mdiv,l7

IO 0,0,_n1,17 ; n1 input

IO 1,1,_n1,17

call ascii_to_hex; conversion returns hex value in rbx

mov [temp_1],rbx ; storing hex in temp_1

IO 0,0,_n1,17 ;n2 input

IO 1,1,_n1,17

call ascii_to_hex

mov [temp_2],rbx ; putting hex of n2 in temp_2

mov rax,[temp_1] ; temp_1->rax

mov rbx,[temp_2] ;temp_2->rbx

xor rdx,rdx

mov rax,[temp_1] ; temp_1->rax

mov rbx,[temp_2] ; temp_2->rbx


div rbx ; div

push rax

push rdx

IO 1,1,mspace,1

IO 1,1,m_rem,m_rem_l

pop rdx

mov rbx,rdx

call hex_to_ascii; remainder output

IO 1,1,mspace,1

IO 1,1,m_qou,m_qou_l

pop rax

mov rbx,rax

call hex_to_ascii; quotient output

ret

ascii_to_hex:

mov rsi, _n1

mov rcx, 16

xor rbx, rbx

next1:

rol rbx, 4

mov al, [rsi]

cmp al,47h

jge error

cmp al, 39h

jbe sub30h

sub al, 7

sub30h:

sub al, 30h

add bl, al

inc rsi

loop next1
ret

hex_to_ascii:

mov rcx, 16

mov rsi,_output

next2:

rol rbx, 4

mov al, bl

and al, 0Fh

cmp al, 9

jbe add30h

add al, 7

add30h:

add al, 30h

mov [rsi], al

inc rsi

loop next2

IO 1,1,_output,16

IO 1,1,mspace,1

ret
Practical No-05
section .data
msg1 db "Count of Positive numbers:"
len1 equ $-msg1
msg2 db "Count of negative numbers:"
len2 equ $-msg2
array db 10,12,-21,-12,-19,-34,41
%macro print 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .bss
count resb 2
pcount resb 2
ncount resb 2
totalcount resb 2
section .text
global _start
_start:
mov byte[count],07
mov byte[pcount],00
mov byte[ncount],00
mov rsi,array
Up:
mov al,00
add al,[rsi]
js neg
inc byte[pcount]
jmp Down
neg:
inc byte[ncount]
Down:
add rsi,01
dec byte[count]
jnz Up
mov bl,[pcount]
mov dl,[ncount]
b1:
print msg1,len1
mov bh,[pcount]
call disp
print msg2,len2
mov bh,[ncount]
call disp
mov rax,60
mov rdi,00
syscall
disp:
mov byte[count],02
loop:
rol bh,04
mov al,bh
AND al,0FH
cmp al,09
jbe l1
add al,07h
l1:add al,30h
mov[totalcount],al
print totalcount,02
dec byte[count]
jnz loop
ret
Practical No-06

global _start

_start:

section .text

; macro for system call for write

%macro disp 2

mov rax,1

mov rdi,1

mov rsi,%1

mov rdx,%2

syscall

%endmacro

; macro for system call for read

%macro accept 2

mov rax,0

mov rdi,0

mov rsi,%1

mov rdx,%2

syscall

%endmacro

;---------First Choice Hex to BCD----------------

ch1:

; accept numbers

disp msg1,len1
accept num,02

call convert

mov [no.1],al

accept num,03

call convert

mov [no.2],al

disp msg2,len2

; Form ax as input

mov ah,[no.1]

mov al,[no.2]

;Point esi to predefined array in .data

mov esi,array1

; Hex to BCD conversion

l5:

mov dx,0000h

mov bx,[esi]

div bx

mov [rem],dx

mov [t1],al

push rsi

call disp_proc

pop rsi

inc esi

inc esi

mov ax,[rem]

dec byte[cnt]

jnz l5
disp msg,len

;To exit program.

ch3:

mov rax,60

mov rdi,0

syscall

;CONVERT procedure

convert:

mov esi,num

mov al,[esi]

cmp al,39h

jle l1

sub al,07h

l1: sub al,30h

rol al,04h ;to swap number

mov bl,al

inc esi

mov al,[esi]

cmp al,39h

jle l2

sub al,07h

l2: sub al,30h

add al,bl

mov [t1],al

ret

;CONVERT2
procedure

convert2:

mov al,[num]

cmp al,39h

jle l8

sub al,07h

l8:sub al,30h

ret

;DISPLAY

procedure

disp_proc:

;for unt's place

mov al,[t1]

cmp al,09h

jle l4

add al,07h

l4:add al,30h

mov [t2],al

disp t2,1

ret

;DISPLAY@ procedure

display2:

mov rsi,charans+3

mov rcx,04h

l12: mov rdx,0

mov rbx,10h

div rbx

cmp dl,09h
jle l3

add dl,07h

l3:add dl,30h

mov [rsi],dl

dec rsi

dec rcx

jnz l12

mov rax,1

mov rdi,1

mov rsi,charans

mov rdx,4

syscall

ret

section .data

msg: db "",10

len: equ $-msg

msg1: db "Enter Hex number : ",10

len1: equ $-msg1

msg2: db "BCD equivalent is : ",10

len2: equ $-msg2

msg3: db "#####MENU#####",10

db "1.Hex to BCD.",10

db "2.BCD to Hex.",10

db "3.Exit.",10

len3: equ $-msg3

msg4: db "Enter your choice : ",10

len4: equ $-msg4

msg5: db "Enter BCD number : ",10


len5: equ $-msg5

msg6: db "Hex equivalent is : ",10

len6: equ $-msg6

array1 dw 2710h,03E8h,0064h,000Ah,0001h

cnt db 5

cnt2 db 5

section .bss

num resb 03

no.1 resb 02

no.2 resb 02

t1 resb 03

t2 resb 03

t3 resb 03

rem resw 02

result resw 03

choice resb 03

charans resb 08
Practical No-07

section .data

msg1:db 'GDTR contents :',0xa

len1:equ $-msg1

msg2:db 'LDTR contents:',0xa

len2:equ $-msg2

msg3:db 'IDTR contents :',0xa

len3:equ $-msg3

msg4:db 'TR contents:',0xa

len4:equ $-msg4

msg5:db 'MSW contents:',0xa

len5:equ $-msg5

msg6:db 'We are in protected mode.!!',0xa

len6:equ $-msg6

msg7:db ' ',0xa

len7:equ $-msg7

msg8:db 'We are not in protected mode.!!',0xa

len8:equ $-msg8

msg9:db ' : ',0xa

len9:equ $-msg9

section .bss
gdt:resd 01

resw 01

ldt:resw 01

idt: resd 01

resw 01

tr:resw 01

msw:resw 01

result: resw 01

section .text

global _start

_start:

smsw [msw]

sgdt [gdt]

sldt [ldt]

sidt [idt]

str [tr]

mov ax,[msw]

bt ax,0

jc next

mov rax,1

mov rdi,1

mov rsi,msg8

mov rdx,len8

syscall

jmp exit
next:

mov rax,1

mov rdi,1

mov rsi,msg6

mov rdx,len6

syscall

;GDTR

mov rax,1

mov rdi,1

mov rsi,msg1

mov rdx,len1

syscall

mov bx,word[gdt+4]

call HtoA

mov bx,word[gdt+2]

call HtoA

mov rax,1

mov rdi,1

mov rsi,msg9

mov rdx,len9

syscall

mov bx,word[gdt]

call HtoA

;LDTR
mov rax,1

mov rdi,1

mov rsi,msg7

mov rdx,len7

syscall

mov rax,1

mov rdi,1

mov rsi,msg2

mov rdx,len2

syscall

mov bx,word[ldt]

call HtoA

;IDTR

mov rax,1

mov rdi,1

mov rsi,msg7

mov rdx,len7

syscall

mov rax,1

mov rdi,1

mov rsi,msg3

mov rdx,len3

syscall
mov bx,word[idt+4]

call HtoA

mov bx,word[idt+2]

call HtoA

mov rax,1

mov rdi,1

mov rsi,msg9

mov rdx,len9

syscall

mov bx,word[idt]

call HtoA

;TR

mov rax,1

mov rdi,1

mov rsi,msg7

mov rdx,len7

syscall

mov rax,1

mov rdi,1

mov rsi,msg4

mov rdx,len4

syscall

mov bx,word[tr]

call HtoA
;MSW

mov rax,1

mov rdi,1

mov rsi,msg7

mov rdx,len7

syscall

mov rax,1

mov rdi,1

mov rsi,msg5

mov rdx,len5

syscall

mov bx,word[msw]

call HtoA

;EXIT

exit:

mov rax,60

mov rdi,0

syscall

HtoA:

mov rcx,4

mov rdi,result

dup1:

rol bx,4

mov al,bl

and al,0fh

cmp al,09h
jg p3

add al,30h

jmp p4

p3: add al,37h

p4:mov [rdi],al

inc rdi

loop dup1

mov rax,1

mov rdi,1

mov rsi,result

mov rdx,4

syscall

ret

You might also like