From 6a5ab3268376d3d4f7d99cedf3160fbe00268aaf Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 10 Jun 2024 11:10:30 +0530 Subject: [PATCH] Added codes. --- Codes/Practical-1.asm | 88 +++++++++ Codes/Practical-10.asm | 145 ++++++++++++++ Codes/Practical-11.asm | 130 ++++++++++++ Codes/Practical-12.asm | 176 +++++++++++++++++ Codes/Practical-13.asm | 153 ++++++++++++++ Codes/Practical-3.1 (with string).asm | 94 +++++++++ Codes/Practical-3.2 (without string).asm | 101 ++++++++++ Codes/Practical-4.1 (with string).asm | 85 ++++++++ Codes/Practical-4.2 (without string).asm | 110 +++++++++++ Codes/Practical-5.1 (successive addition).asm | 119 +++++++++++ .../Practical-5.2 (add and shift method).asm | 186 ++++++++++++++++++ Codes/README.md | 5 + 12 files changed, 1392 insertions(+) create mode 100644 Codes/Practical-1.asm create mode 100644 Codes/Practical-10.asm create mode 100644 Codes/Practical-11.asm create mode 100644 Codes/Practical-12.asm create mode 100644 Codes/Practical-13.asm create mode 100644 Codes/Practical-3.1 (with string).asm create mode 100644 Codes/Practical-3.2 (without string).asm create mode 100644 Codes/Practical-4.1 (with string).asm create mode 100644 Codes/Practical-4.2 (without string).asm create mode 100644 Codes/Practical-5.1 (successive addition).asm create mode 100644 Codes/Practical-5.2 (add and shift method).asm create mode 100644 Codes/README.md diff --git a/Codes/Practical-1.asm b/Codes/Practical-1.asm new file mode 100644 index 0000000..6f4d683 --- /dev/null +++ b/Codes/Practical-1.asm @@ -0,0 +1,88 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to count number of positive and negative numbers from the array. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 +mov rax,1 ; Standard output +mov rdi,1 ; Input write +mov rsi,%1 ; Display message address +mov rdx,%2 ; Message length +syscall ; Interrupt for kernel in 64-bit +%endmacro + +section .data + +m1 db 10,"ALP to count positive and negative numbers from an array",10 +l1 equ $-m1 + +m2 db 10,"The count of positive numbers is:",10 +l2 equ $-m2 + +m3 db 10,"The count of negative numbers is:",10 +l3 equ $-m3 + +;array dw 8132h,6879h,711Ah,3567h,4567h +;array dd 81328132h,12346879h,6735711Ah,34563567h,67894567h +array dq 81328132ABCDh,12346879AAAAh,6735711AFFFFh,34563567EEEEh,67894567DEFAh + +newline db 10 +pcnt db 00 +ncnt db 00 + +section .bss +displaybuffer resb 2 + +section .text + +global _start: +_start: + +print m1,l1 + +mov rsi,array +mov rcx,05 + +up: +bt qword [rsi],63 ; Check the most significant bit for negativity +jnc pnxt ; If not negative, jump to pnxt +inc byte[ncnt] ; Increment negative count +jmp pskip ; Jump to pskip +pnxt: inc byte[pcnt] ; Increment positive count +pskip: add rsi,8 ; Move to next 8 bytes +loop up ; Loop until rcx becomes zero + +print m2,l2 +mov bl,[pcnt] +call display +print newline,1 + +print m3,l3 +mov bl,[ncnt] +call display +print newline,1 + +mov rax,60 +syscall + +display: + mov rdi,displaybuffer ; Destination for displaying + mov rcx,02 ; Display 2 characters +dloop: + rol bl,04 ; Rotate left by 4 bits + mov dl,bl ; Move contents of bl to dl + and dl,0fh ; Mask out upper bits + add dl,30h ; Convert to ASCII + cmp dl,39h ; Compare if greater than '9' + jbe skip ; Jump if below or equal + add dl,07h ; Adjust for letters +skip: + mov [rdi],dl ; Store the character + inc rdi ; Move to next position + loop dloop ; Loop until rcx becomes zero +print displaybuffer,2 ; Print the display buffer +ret +; END OF CODE diff --git a/Codes/Practical-10.asm b/Codes/Practical-10.asm new file mode 100644 index 0000000..76d4b27 --- /dev/null +++ b/Codes/Practical-10.asm @@ -0,0 +1,145 @@ +; THIS CODE HAS NOT BEEN TESTED AND IS NOT FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to switch from real mode to protected mode and display the values of GDTR, LDTR, IDTR, TR and MSW Registers also identify CPU type using CPUID instruction. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +section .data + rmodemsg db 10,'Processor is in REAL MODE.', + rmsg_len:equ $-rmodemsg + + pmodemsg db 10,'Processor is in PROTECTED MODE.' + pmsg_len:equ $-pmodemsg + + gdtmsg db 10,'GDT Contents are: ' + gmsg_len:equ $-gdtmsg + + ldtmsg db 10,'LDT Contents are: ' + lmsg_len:equ $-ldtmsg + + idtmsg db 10,'IDT Contents are: ' + imsg_len:equ $-idtmsg + + trmsg db 10,'Task Register Contents are: ' + tmsg_len: equ $-trmsg + + mswmsg db 10,'Machine Status Word: ' + mmsg_len:equ $-mswmsg + + colmsg db ':' + + nwline db 10 + +section .bss + gdt resd 1 + resw 1 + ldt resw 1 + idt resd 1 + resw 1 + tr resw 1 + + cr0_data resd 1 + + dnum_buff resb 04 + +%macro print 2 + mov rax,01 + mov rdi,01 + mov rsi,%1 + mov rdx,%2 + syscall +%endmacro + +section .text +global _start +_start: + smsw eax ;Reading CR0. As MSW is 32-bit cannot use RAX register. + + mov [cr0_data],rax + + bt rax,0 ;Checking PE bit, if 1=Protected Mode, else Real Mode + jc prmode + print rmodemsg,rmsg_len + jmp nxt1 + +prmode: print pmodemsg,pmsg_len + +nxt1:sgdt [gdt] + sldt [ldt] + sidt [idt] + str [tr] + print gdtmsg,gmsg_len + + mov bx,[gdt+4] + call print_num + + mov bx,[gdt+2] + call print_num + + print colmsg,1 + + mov bx,[gdt] + call print_num + + print ldtmsg,lmsg_len + mov bx,[ldt] + call print_num + + print idtmsg,imsg_len + + mov bx,[idt+4] + call print_num + + mov bx,[idt+2] + call print_num + + print colmsg,1 + + mov bx,[idt] + call print_num + + print trmsg,tmsg_len + + mov bx,[tr] + call print_num + + print mswmsg,mmsg_len + + mov bx,[cr0_data+2] + call print_num + + mov bx,[cr0_data] + call print_num + + print nwline,1 + + +exit: mov rax,60 + xor rdi,rdi + syscall + +print_num: + mov rsi,dnum_buff ;point esi to buffer + + mov rcx,04 ;load number of digits to printlay + +up1: + rol bx,4 ;rotate number left by four bits + mov dl,bl ;move lower byte in dl + and dl,0fh ;mask upper digit of byte in dl + add dl,30h ;add 30h to calculate ASCII code + cmp dl,39h ;compare with 39h + jbe skip1 ;if less than 39h skip adding 07 more + add dl,07h ;else add 07 +skip1: + mov [rsi],dl ;store ASCII code in buffer + inc rsi ;point to next byte + loop up1 ;decrement the count of digits to printlay + ;if not zero jump to repeat + + print dnum_buff,4 ;printlay the number from buffer + + ret +; END OF CODE diff --git a/Codes/Practical-11.asm b/Codes/Practical-11.asm new file mode 100644 index 0000000..c1d5a7d --- /dev/null +++ b/Codes/Practical-11.asm @@ -0,0 +1,130 @@ +; THIS CODE HAS NOT BEEN TESTED AND IS NOT FULLY OPERATIONAL. + +; Problem Statement: Write x86 ALP to find the factorial of a given integer number on a command line by using recursion. Explicit stack manipulation is expected in the code. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 +mov rax,1 +mov rdi,1 +mov rsi,%1 +mov rdx,%2 +syscall +%endmacro + +%macro exitprog 0 +mov rax,60 +xor rdi,rdi +syscall +%endmacro + +%macro gtch 1 +mov rax,0 +mov rdi,0 +mov rsi,%1 +mov rdx,1 +syscall +%endmacro + +section .data +nwline db 10 +m0 db 10,13,"----- Program to calculate factorial of a given number -----",10,10 +l0 equ $-m0 +m2 db 10,"Enter a number (2 digit HEX no.): " +l2 equ $-m2 +m4 db 10,"The factorial is: " +l4 equ $-m4 +factorial dq 1 + +section .bss +no1 resq 1 +input resb 1 +output resb 1 + +section .text +global _start +_start: + +print m0,l0 +print m2,l2 +call getnum + +mov [no1],rax ;accept number +gtch input ;to read and discard ENTER key ;pressed + +mov rcx,[no1] + +call facto +mov rax,00 + +print m4,l4 +mov rax,qword[factorial] +call disphx16 ;displays a 8 digit hex number ;in rax + +exitprog + +facto: +push rcx +cmp rcx,01 +jne ahead +jmp exit2 + +ahead: dec rcx + +call facto + +exit2: +pop rcx +mov rax,rcx +mul qword[factorial] +mov qword[factorial],rax +ret +;Procedure to get a 2 digit hex no from user +;number returned in rax + +getnum: +mov cx,0204h +mov rbx,0 +ll2: +push rcx +gtch input +pop rcx +mov rax,0 +mov al,byte[input] +sub rax,30h +cmp rax,09h +jbe skip1 +sub rax,7 + +skip1: +shl rbx,cl +add rbx,rax +dec ch +jnz ll2 +mov rax,rbx +ret + +disphx16: ;displays a 16 digit hex number +mov rbx,rax +mov cx,1004h ;16 digits to display and 04 count ; to rotate + +ll6: +rol rbx,cl +mov rdx,rbx +and rdx,0fh +add rdx,30h +cmp rdx,039h +jbe skip4 +add rdx,7 + +skip4: +mov byte[output],dl +push rcx +print output,1 +pop rcx +dec ch +jnz ll6 +ret +; END OF CODE diff --git a/Codes/Practical-12.asm b/Codes/Practical-12.asm new file mode 100644 index 0000000..70273d6 --- /dev/null +++ b/Codes/Practical-12.asm @@ -0,0 +1,176 @@ +; THIS CODE HAS NOT BEEN TESTED AND IS NOT FULLY OPERATIONAL. + +; Problem Statement: Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation. Define the input values in data segment. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 + mov rax,01 + mov rdi,01 + mov rsi,%1 + mov rdx,%2 + syscall +%endmacro + + +section .data + m0 db 10,"------ Program to calculate mean, variance, standard deviation -----", 10 + l0:equ $-m0 + + m1 db 10,"Mean is: " + l1:equ $-m1 + + m2 db 10,"Variance is: " + l2:equ $-m2 + + m3 db 10,"Standard Deviation is: " + l3:equ $-m3 + + m4 db 10,"Values are: 102.59, 198.21, 100.67" + l4:equ $-m4 + + dpoint db "." + + hdec dq 100 + + num1 dd 102.59 + num2 dd 198.21 + num3 dd 100.67 + num4 dd 3.00 + + newline db 0xa + +section .bss + + dispbuff resb 1 + resbuff resb 10 ;or we can also write resbuff rest 1 + mean resd 1 + variance resd 1 + + +section .text + + global _start + _start: + print m0,l0 + print m4,l4 + + finit ;initialize coprocessor + fldz ;load stack top 0 + + ;logic to calculate mean + + fld dword[num1] ;first number on x387 + fld dword[num2] + + fadd st0,st1 + + fld dword[num3] + fadd st0,st1 + + fdiv dword[num4] ;st0=add/3=mean + fst dword[mean] + + print m1,l1 + + call disp_result + + print newline,01 ;newline + + ;logic to calculate variance + + mov rsi,num1 + call cal_diff_sqr + + mov rsi,num2 + call cal_diff_sqr + + fadd st0,st1 + + mov rsi,num3 + call cal_diff_sqr + + fadd st0,st1 + + fdiv dword[num4] ;divide by cardinality + + fst dword[variance] + + print m2,l2 + + call disp_result + + print newline,01 ;newline + + ;logic to calculate standard deviation + + fld dword[variance] + fsqrt + + print m3,l3 + call disp_result + + print newline,01 ;newline + + mov rax,60 + mov rdi,0 + syscall + + disp_result: + fimul dword[hdec] + fbstp [resbuff] ;store bcd and pop from top of the stack + + xor rcx,rcx ;clear rcx register + mov rcx,09h + mov rsi,resbuff+9 ;rsi pointing to msb of resultant + + up1: + push rcx + push rsi + + mov bl,[rsi] + call disp8_proc + + print dispbuff,02 + pop rsi + dec rsi + pop rcx + loop up1 + + print dpoint,01 + + mov bl,[resbuff] + call disp8_proc + + print dispbuff,02 + + ret + +disp8_proc: + mov rdi,dispbuff + mov rcx,02 + + back: + rol bl,04 + mov dl,bl + and dl,0Fh + cmp dl,09h + jbe skip + add dl,07h + +skip: + add dl,30h + mov [rdi],dl + inc rdi + loop back + + ret + +cal_diff_sqr: + fld dword[rsi] + fsub dword[mean] + fmul st0,st0 + ret +; END OF CODE diff --git a/Codes/Practical-13.asm b/Codes/Practical-13.asm new file mode 100644 index 0000000..076f949 --- /dev/null +++ b/Codes/Practical-13.asm @@ -0,0 +1,153 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write 80387 ALP to find the roots of the quadratic equation. All the possible cases must be considered in calculating the roots. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +section .data +msg1 db "Complex Root",10 +msglen1 equ $-msg1 +msg2 db "Root 1: " +msglen2 equ $-msg2 +msg3 db 10, "Root 2: " +msglen3 equ $-msg3 +a dd 1.00 +b dd -6.00 +c dd 8.00 +four dd 4.00 +two dd 2.00 + +hdec dq 100 +point db "." + +section .bss + + root1 resd 1 + root2 resd 1 + resbuff rest 1 + temp resb 2 + disc resd 1 + +%macro write 2 ;macro for display + mov rax,1 + mov rdi,1 + mov rsi,%1 + mov rdx,%2 + syscall +%endmacro + +%macro read 2 ;macro for input + mov rax,0 + mov rdi,0 + mov rsi,%1 + mov rdx,%2 + syscall +%endmacro + + +%macro exit 0 ;macro for exit + mov rax,60 + xor rdi,rdi + syscall +%endmacro + +section .text + global _start + _start: + + finit ;initialise 80387 co-processor + fld dword[b] ;stack: b + + fmul dword[b] ;stack: b*b + + fld dword[a] ;stack: a, b*b + + fmul dword[c] ;stack: a*c, b*b + + fmul dword[four] ;stack: 4*a*c,b*b + + fsub ;stack: b*b - 4*a*c + + ftst ;compares ST0 and 0 + jb no_real_solutions + + fsqrt ;stack: sqrt(b*b - 4*a*c) + fst dword[disc] ;store disc= sqrt(b*b - 4*a*c) + + fsub dword[b] ;stack: disc-b + + fdiv dword[a] ;stack: disc-b/2*a or (-b+disc)/2a + + + fdiv dword[two] + + write msg2,msglen2 + + call disp_proc + + fldz ;stack:0 + fsub dword[disc] ;stack:-disc + fsub dword[b] ;stack: -disc - b + fdiv dword[a] ;stack: (-b - disc)/(2*a) + fdiv dword[two] + + write msg3,msglen3 + call disp_proc + jmp exi + +no_real_solutions: +write msg1,msglen1 +exi : + +mov rax,60 +mov rdi,1 +syscall + + +disp_proc: + FIMUL dword[hdec] + FBSTP tword[resbuff] + mov rsi,resbuff+9 + mov rcx,09 + next1: + + push rcx + push rsi + + mov bl,[rsi] + call disp + + pop rsi + pop rcx + + dec rsi + loop next1 + + push rsi + write point,1 + pop rsi + mov bl,[rsi] + call disp + ret + + +disp: + mov rdi,temp ;mov dnum address into edi + mov rcx,02 ;initialize ecx with 2 + dispup1: + rol bl,4 ;rotate bl by 4 bits + mov dl,bl ;move bl into dl + and dl,0fh ;and of dl with 0fh + add dl,30h ;add 30h into dl + cmp dl,39h ;compare dl with 39h + jbe dispskip1 ;jump if below and equal to dispskip1 + add dl,07h ;add 7h into dl + dispskip1: + mov [rdi],dl ;mov dl into dnum + inc rdi ;increament edi by a byte + loop dispup1 ;loop dispup1 while ecx not zero + write temp,2 ;Display dnum by calling macro + ret ;return from procedure +; END OF CODE diff --git a/Codes/Practical-3.1 (with string).asm b/Codes/Practical-3.1 (with string).asm new file mode 100644 index 0000000..06ee643 --- /dev/null +++ b/Codes/Practical-3.1 (with string).asm @@ -0,0 +1,94 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform non-overlapped block transfer (WITH string specific instructions). Block containing data can be defined in the data segment. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 +mov rax,1 ; Standard output +mov rdi,1 ; Input write +mov rsi,%1 ; Display message address +mov rdx,%2 ; Message length +syscall ; Interrupt for kernel in 64-bit +%endmacro + +section .data +msg db 10, 'Block contents before transfer',10,13 ; Initializing the display message +msglen equ $-msg ; Length of the message + +msg2 db 10, 'Block contents after transfer',10,13 ; Initializing the display message +msg2len equ $-msg2 ; Length of the message + +msg3 db 10,13, 'Source block' +msg3len equ $-msg3 + +msg4 db 10,13, 'Destination block' +msg4len equ $-msg4 + +space db ' ' ; Space between the variables to be displayed +spacelen equ $-space ; Initializing length of space + +srcblk db 10h,20h,30h,40h,50h ; Contents of the src block +destblk db 0,0,0,0,0 ; Initial contents of dest block +cnt equ 5 ; Count is equal to 5 as 5 variables declared + +section .bss ; Storing the array of data/reserving space for the data +ans resb 4 ; Reserve buffer ans of 4-bytes(8-bits each) +;destblk resb 5 ; Reserve buffer destblk of 5-bytes + +section .text +global _start ; Starting of the main program +_start: + print msg,msglen ; Displaying the msg1 + mov rsi,srcblk ; Move contents of source blk to rsi + call disp_block ; Calling procedure disp_block + cld ; Clear direction flag + mov rcx,05h ; Move 05h into rcx register + mov rsi,srcblk ; Move contents of src block into rsi + mov rdi,destblk ; Move contents of dest blok into rdi + rep movsb ; Repeat move string byte + + print msg2,msg2len ; Displaying the msg2 + mov rsi,destblk ; Move contents of destblk into rsi + call disp_block ; Calling procedure disp_block + mov rax,60 ; Exit system call + xor rdi,rdi ; Clearing rdi (by using xor) so that we get 0 in destblk + syscall ; Interrupt for kernel in 64-bit + +disp_block: ; Procedure disp_block + mov rbp,cnt ; Base pointers + +back: + mov al,[rsi] ; Move contents of rsi to al + push rsi ; Push contents of rsi + call disp_8 ; Calling the disp_8 procedure + print space,1 ; Intialize one byte for space + pop rsi ; Pop contents of rsi + inc rsi ; Increment rsi by 1 + dec rbp ; Decrement cnt through rbp + jnz back ; Jump to loop back if not zero + ret ; Return + +disp_8: ; Procedure disp_8 + mov rsi,ans+1 ; Move the contents of ans buffer into rsi + mov rcx,2 ; Move 2 into the rcx register + +back1: ; Loop back1 + mov rdx,0 ; Clear rdx + mov rbx,16 ; Move 16 into rbx register + div rbx ; Divide rax register by rbx register + cmp dl,09h ; Compare the contents of dl register with 09h + jbe add_30 ; If compared value is below (less) than add 30h + add dl ,07h ; Add 07h to dl register + +add_30: ; add_30 label + add dl,30h ; Add 30h to the contents of dl register + mov[rsi],dl ; Move contents of dl into rsi + dec rsi ; Decrement rsi + dec rcx ; Decrement rcx + jnz back1 ; Jump if not zero to loop back1 + print ans,2 ; Print the result +ret ; Return +; END OF CODE diff --git a/Codes/Practical-3.2 (without string).asm b/Codes/Practical-3.2 (without string).asm new file mode 100644 index 0000000..7b4dc5e --- /dev/null +++ b/Codes/Practical-3.2 (without string).asm @@ -0,0 +1,101 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform non-overlapped block transfer (WITHOUT string specific instructions). Block containing data can be defined in the data segment. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 +mov rax,1 +mov rdi,1 +mov rsi,%1 +mov rdx,%2 +syscall +%endmacro + +section .data +m1 db 10,"Source block:",10,13 +m1len equ $-m1 + +m2 db 10,"Destination block after transfer:",10,13 +m2len equ $-m2 + +space db" " +spacelen equ $-space + +srcblk db 10h,20h,30h,40h,50h + +count equ 05h + +section .bss +ans resb 4 +dstblk resb 5 + +section .text +global _start +_start: +print m1,m1len +mov rsi,srcblk +call disp_block + +print m2,m2len + +mov rsi,srcblk +mov rdi,dstblk +mov rcx,05 + +s1: mov al,[rsi] +mov [rdi],al +inc rsi +inc rdi +loop s1 + +mov rsi,dstblk +call disp_block + +mov rax,60 +xor rdi,rdi +syscall + +disp_block: +mov rbp,count +back:mov al,[rsi] +push rsi +call disp_8 +print space,1 +pop rsi +inc rsi +dec rbp +jnz back +ret + +disp_8:mov rsi,ans + +mov bl,al +mov dl,bl +rol dl,04 +and dl,0fh +cmp dl,09h +jbe add30 +add dl,07h + +add30: +add dl,30h +mov [rsi],dl +inc rsi + +mov dl,bl +and dl,0fh +cmp dl,09h +jbe add130 +add dl,07h + +add130: + +add dl,30h +mov [rsi],dl + +print ans,2 +ret +; END OF CODE diff --git a/Codes/Practical-4.1 (with string).asm b/Codes/Practical-4.1 (with string).asm new file mode 100644 index 0000000..1af3924 --- /dev/null +++ b/Codes/Practical-4.1 (with string).asm @@ -0,0 +1,85 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform overlapped block transfer (WITH string specific instructions). Block containing data can be defined in the data segment. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 +mov rax,1 +mov rdi,1 +mov rsi,%1 +mov rdx,%2 +syscall +%endmacro + +section .data +msg1 db 10,"src blk",10,13 ; Initializing the display message +msg1len equ $-msg1 ; Initializing the length of message +msg2 db 10,"dest blk",10,13 +msg2len equ $-msg2 +srcblk db 10h,20h,30h,40h,50h ; Storing variables in source block +cnt equ 05h ; Count is equal to 5 as 5 variables declared +space db " " ; Space between the variables to be displayed +spacelen equ $-space ; Initializing length of space + +section .bss ; Storing the array of data/reserving space for the data +ans resb 4 ; Reserve buffer ans of 4-bytes(8-bits each) +destblk resb 5 ; Reserve buffer destblk of 5-bytes + +section .text +global _start ; Starting of the main program +_start: +print msg1,msg1len ; Displaying the msg1 +mov rsi,srcblk ; Move contents of source blk to rsi +call disp_block ; Calling procedure disp_block +cld ; Clear direction flag +mov rcx,02h ; Move 02h into rcx register +mov rsi,srcblk ; Move contents of the source blk to the rsi +mov rdi,destblk ; Shift 2 positions in the source blk and move the contents into rdi +rep movsb ; Repeat move string byte +mov rcx,03h ; Move 02h into rcx register +mov rsi,srcblk ; Move contents of the source blk to the rsi +mov rdi,destblk+2 +rep movsb +print msg2,msg2len ; Displaying the msg2 +mov rsi,destblk ; Move contents of destblk into rsi +call disp_block ; Calling procedure disp_block +mov rax,60 ; Exit system call +xor rdi,rdi ; Clearing rdi (by using xor) so that we get 0 in destblk +syscall ; Interrupt for kernel in 64-bit +disp_block: ; Procedure disp_block +mov rbp,cnt ; Base pointers +back: +mov al,[rsi] ; Move contents of rsi to al +push rsi ; Push contents of rsi +call disp_8 ; Calling the disp_8 procedure +print space,1 ; Initialize one byte for space +pop rsi ; Pop contents of rsi +inc rsi ; Increment rsi by 1 +dec rbp ; Decrement cnt through rbp +jnz back ; Jump to loop back if not zero +ret ; Return + +disp_8: ; Procedure disp_8 + mov rsi,ans+1 ; Move the contents of ans buffer into rsi + mov rcx,2 ; Move 2 into the rcx register + +back1: ; Loop back1 + mov rdx,0 ; Clear rdx + mov rbx,16 ; Move 16 into rbx register + div rbx ; Divide rax register by rbx register + cmp dl,09h ; Compare the contents of dl register with 09h + jbe add_30 ; If compared value is below (less) than add 30h + add dl ,07h ; Add 07h to dl register + +add_30: ; Add_30 label + add dl,30h ; Add 30h to the contents of dl register + mov[rsi],dl ; Move contents of dl into rsi + dec rsi ; Decrement rsi + dec rcx ; Decrement rcx + jnz back1 ; Jump if not zero to loop back1 + print ans,2 ; Print the result + ret +; END OF CODE diff --git a/Codes/Practical-4.2 (without string).asm b/Codes/Practical-4.2 (without string).asm new file mode 100644 index 0000000..a82d59b --- /dev/null +++ b/Codes/Practical-4.2 (without string).asm @@ -0,0 +1,110 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform overlapped block transfer (WITHOUT string specific instructions). Block containing data can be defined in the data segment. + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 2 ; Defined macro for printing messages 2 stand for the parameters +mov rax,1 ; For system write specifier +mov rdi,1 ; Standard output +mov rsi,%1 +mov rdx,%2 +syscall +%endmacro + +section .data +m1 db 10,"source block",10,13 ; 10 stands for new line and 13 stands for tab +m1len equ $-m1 + +m2 db 10,"destination block after transfer:",10,13 +m2len equ $-m2 + +space db " " +spacelen equ $-space + +srcblk db 10h,20h,30h,40h,50h ; srcblk defined for the functioning +count equ 05h ; count initialized to 5 + +section .bss +ans resb 4 ; ans block is defined for storing the transformed value of the numbers from hexa to ascii +dstblk resb 5 ; dstblk is our destination block + +section .text ; This section is necessary for assembler understanding +global _start +_start: +print m1,m1len +mov rsi,srcblk ; rsi now points to the first location of the source block +call disp_block ; Calling the procedure for displaying the source block + +print m2,m2len + +mov rsi,srcblk ; Now the rsi points to the source block +mov rdi,dstblk ; Now the destination pointer points to the first memory location of the destination block +mov rcx,02 ; rcx is the register used for the counter purpose + +s1: mov al,[rsi] +mov [rdi],al +inc rsi +inc rdi +loop s1 +mov rsi,srcblk +mov rcx,03 + +s2: mov al,[rsi] +mov [rdi],al +inc rsi +inc rdi +loop s2 + +mov rsi,dstblk +call disp_block + +mov rax,60 +xor rdi,rdi +syscall + +disp_block: +mov rbp,count + +back: +mov al,[rsi] +push rsi +call disp_8 +print space,1 +pop rsi +inc rsi +dec rbp +jnz back +ret + +disp_8: +mov rsi,ans + +mov bl,al +mov dl,bl +rol dl,04 +and dl,0fh +cmp dl,09h +jbe add30 +add dl,07h + +add30: +add dl,30h +mov [rsi],dl +inc rsi + +mov dl,bl +and dl,0fh +cmp dl,09h +jbe add130 +add dl,07h + +add130: +add dl,30h +mov [rsi],dl + +print ans,2 +ret +; END OF CODE diff --git a/Codes/Practical-5.1 (successive addition).asm b/Codes/Practical-5.1 (successive addition).asm new file mode 100644 index 0000000..851ee4d --- /dev/null +++ b/Codes/Practical-5.1 (successive addition).asm @@ -0,0 +1,119 @@ +; THIS CODE HAS NOT BEEN TESTED AND IS NOT FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Using successive addition method. (Use of 64-bit registers is expected). + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro print 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 .data + +m1 db 10,"----- Multiplication using successive addition -----",10,13 +l1 equ $-m1 + +m2 db 10,"Enter the multiplicand",10,13 +l2 equ $-m2 + +m3 db 10,"Enter the multiplier",10,13 +l3 equ $-m3 + +res db 10,"Multiplication of the numbers is::",10,13 +rlen equ $-res + + +section .bss + +numascii resb 03 +mplier resq 02 +mcand resq 02 +;result resq 01 +dispbuff resb 04 + +section .txt +global _start: +_start: + +print m1,l1 + +print m2,l2 +accept numascii,3 +call packnum +mov byte[mcand],bl + +print m3,l3 +accept numascii,3 +call packnum +mov byte[mplier],bl + +print res,rlen + + mov rax,0 + cmp qword[mplier],0 + jz ll5 + +ll1: + add rax,qword[mcand] + dec qword[mplier] + jnz ll1 + +ll5: + call dispnum + +exit: + mov rax,60 + mov rbx,00 + syscall + + +dispnum: + mov rbx,rax + mov rcx,04 + mov rdi,dispbuff + up2: + rol bx,4 + mov al,bl + and al,0fh + cmp al,09 + jbe dskip + add al,07h +dskip: + add al,30h + mov [rdi],al + inc rdi + loop up2 + print dispbuff,4 + ret + + +packnum: + mov bl,0 + mov rcx,2 + mov rsi,numascii + up1: + rol bl,04 + mov al,[rsi] + cmp al,39h + jbe skip1 + sub al,07h + skip1: + sub al,30h + add bl,al + inc rsi + loop up1 + ret diff --git a/Codes/Practical-5.2 (add and shift method).asm b/Codes/Practical-5.2 (add and shift method).asm new file mode 100644 index 0000000..c327dd9 --- /dev/null +++ b/Codes/Practical-5.2 (add and shift method).asm @@ -0,0 +1,186 @@ +; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +; Problem Statement: Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Using add and shift method. (Use of 64-bit registers is expected). + +; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/ + + +; BEGINNING OF CODE +%macro dispmsg 2 ;macro for display + mov rax,1 ;standard ouput + mov rdi,1 ;system for write + mov rsi,%1 ;display message address + mov rdx,%2 ;display message length + syscall ;interrupt for 64-bit +%endmacro ;close macro + +%macro exitprog 0 ;macro for exit + mov rax,60 ;system for exit + mov rdx,0 + syscall ;interrupt for 64-bit +%endmacro ;close macro + +%macro gtch 1 ;macro for accept + mov rax,0 ;standard input + mov rdi,0 ;system for read + mov rsi,%1 ;input the message + mov rdx,1 ;message length + syscall ;interrupt for 64-bit +%endmacro ;close macro + +;------------------------------ +section .data + nwline db 10 + m0 db 10,10,"Program to multiply two numbers using successive addition and add-and-shift method" + l0 equ $-m0 + m1 db 10,"1. Add-and-Shift method",10,"2. Exit",10,10, "Choose an option (1/2 ): " + l1 equ $-m1 + m2 db 10,"Enter multiplicand (2 digit HEX no): " + l2 equ $-m2 + m3 db 10,"Enter multiplier (2 digit HEX no): " + l3 equ $-m3 + m4 db 10,"Multiplication is: " + l4 equ $-m4 +;------------------------------ + +;------------------------------ +section .bss + mcand resq 1 ;reserve 1 quad for multiplicand + mplier resq 1 ;reserve 1 quad for multiplier + input resb 1 ;reserve 1 byte for input + output resb 1 ;reserve 1 byte for output + choice resb 1 ;reserve 1 byte for choice +;------------------------------ + +;------------------------------ +section .text +global _start ;starting of main program +_start : + +dispmsg m0,l0 ;displaying the menu + +back: +dispmsg m1,l1 ;displaying the first message +gtch input ;to read and discard ENTER key pressed. + +mov al, byte[input] ;get choice +mov byte[choice],al + +gtch input ;to read and discard ENTER key pressed. + +mov al, byte[choice] + +cmp al, '1' ;compare contents of al with 1 +je shft_add ;if equal the jump to succ_add procedure + +cmp al, '2' ;compare the contents of al with 3 +jnz back ;if not zero then jump to back +exitprog ;exit program +;------------------------------ + +;-------- ADD & SHIFT --------- +shft_add: ;shft_add procedure +dispmsg m2,l2 ;Displaying the second message +call getnum ;call getnum procedure +mov [mcand],rax ;mov contents of rax(multiplicand) into mcand buffer +gtch input ;to read and discard ENTER key pressed + +dispmsg m3,l3 ;Displaying the third message +call getnum ;call getnum procedure +mov [mplier],rax ;mov contents of rax(multiplier) into mplier buffer +gtch input ;to read and discard ENTER key pressed + +mov rax,0 ;clearing the rax register +dispmsg m4,l4 ;displaying the fourth message + +mov rax,0 ;clearing the rax register +mov rcx,8 ;taking count of 8 in rcx register +mov dl,[mplier] ;multiplier is 8 bits so it occupies dl +mov bl,[mcand] ;mupltiplicand is 8 bits so it occupies bl + +;we will put Q in higher 8 bits of ax (i.e. ah) +;and multipler in lower 8 bits of ax (i.e. al) + +mov ah,0 ;clearing ah register +mov al,dl ;ah already 0 and al now contains multiplier + +;------------------------------ + +ll3: ;loop 3 (s3) +mov dh,al ;mov contents of al into dh as dh is used as temporary +and dh,1 ;check d0 bit of multiplier +jz ll8 ;if d0 bit was zero, Z flag will be set (s2)(if zero jmp to loop 8) +add ah, bl ;d0 bit of multiplier is set +;so add multiplicand to Q(add bl into ah) + +;------------------------------ + +ll8: ;loop 8 (s2) +shr ax,1 ;shift both Q (ah) and muplitiplier (al) right 1 bit +dec rcx ;decrement contents of rcx +jnz ll3 ;if not zero then jump to loop 3 (s3) +call disphx16 ;call procedure disphx16 +jmp back ;jump to back + +;------------------------------ + +getnum: ;procedure to get a 2 digit hex no from user +; number returned in rax + +mov cx,0204h ;02 digits to display and 04 count to rotate +mov rbx,0 ;clearing rbx register + +;------------------------------ + +ll2: ;loop 2 +push rcx ;syscall destroys rcx.Rest all regs are preserved +gtch input ;to read and discard ENTER key pressed +pop rcx ;pop the contents of rcx + +mov rax,0 ;clearing the contents of rax +mov al,byte[input] ;get choice +sub rax,30h ;subtract 30h from contents of rax +cmp rax,09h ;compare the contents of rax register with 09h +jbe skip1 ;if equal then jump below to skip1 label +sub rax,7 ;subtract 7 from contents of rax register + +;------------------------------ + +skip1: ;skip1 label +shl rbx,cl ;shift multiplicand and count to the left +add rbx,rax ;add contents of rax register to the contents of rbx register +dec ch ;decrement the contents of ch register +jnz ll2 ;if not zero then jump to loop 2 +mov rax,rbx ;mov contents of rbx register into rax register +ret ;return + +;------------------------------ + +disphx16: ;Displays a 16 digit hex number passed in rax +mov rbx,rax ;move contents of rax register into rbx register +mov cx,1004h ;16 digits to display and 04 count to rotate + +;------------------------------ + +ll6: ;loop 6 +rol rbx,cl ;rotate multiplicand and count to the left +mov rdx,rbx ;mov contents of rbx register into rdx register +and rdx,0fh ;anding contents of rdx register with 0fh +add rdx,30h ;adding contents of rdx register with 30h +cmp rdx,039h ;comparing the contents of rdx register with 39h +jbe skip4 ;if equal then jump below to skip4 label +add rdx,7 ;add 7 to the contents of rdx register + +;------------------------------ + +skip4: ;skip4 label +mov byte[output],dl ;mov contents of dl register into output buffer in bytes +push rcx ;push the contents of rcx register +dispmsg output,1 ;displaying the output +pop rcx ;pop the contents of rcx +dec ch ;decrement the count(contents of ch) +jnz ll6 ;if not zero the jump to loop 6 +ret ;return + +;------------------------------ +; END OF CODE diff --git a/Codes/README.md b/Codes/README.md new file mode 100644 index 0000000..4c99552 --- /dev/null +++ b/Codes/README.md @@ -0,0 +1,5 @@ +# CODES NOT HERE? + +There are some codes that haven't been tested yet available in the [testing branch](https://git.kska.io/sppu-se-comp-content/Microprocessor/src/branch/testing), check it out, you might find 'em! + +---