Friday, February 15, 2019


Write X86 program to sort the list of integers in ascending/descending order. Read the input from the text file and write the sorted data back to the same text file using bubble sort

 Algorithm for Bubble Sort
Bubble sort algorithm works by comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you need not swap the element. Then, again second and third elements are compared and swapped if it is required and this process go on until last and second last element is compared and swapped. This completes the first pass of bubble sort. By doing this we bubble out the largest element at the last location in the array.
If there are n elements to be sorted then, the process mentioned above should be repeated n times to get the sorted array. As, in each pass one element is bubbled out. So to place all the elements in the unsorted array at their proper place we need to repeat the above said process for n times.
 Following figure will give you better understanding of bubble sort:


Figure: working of Bubble Sort


j=bufferlen-1
while(j>0)
{
        i=cnt1=bufferlen-1
        while(i>0)
        {
            if(a[i]>a[i+1])
                  swap(a[i],a[i+1])
            i--;
        }
j--;
}



b_sort2.asm (64-bit nasm code file) 

%macro file 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro

Section .data
accept_fn db "Enter file name",10,13
accept_fn_len equ $-accept_fn

openmsg db 10,13,"File Opened Successfully"
openmsg_len equ $-openmsg

closemsg db 10,13,"File Closed Successfully"
closemsg_len equ $-closemsg

errormsg db 10,13,"Failed to open file", 0xa
errormsg_len equ $-errormsg

space db " "
space_len equ $ - space

sortmsg db 10,13,"After Sorting "
sortmsg_len equ $-sortmsg

Section .bss
buffer resb 200
bufercpy resb 200
bufferlen resb 8
filename resb 50
cnt1 resb 8
cnt3 resb 8
fdisplay resb 8

Section .text
global _start
_start:
file 1,1,accept_fn,accept_fn_len
file 0,0,filename,50
dec    rax
mov    byte[filename + rax],0           ; blank char/null char

file 2, filename, 2, 777         ; Opening filein read-write mode with permission to read write   
                                              and execute to all(owner, group and other)

mov qword[fdisplay], rax      ;RAX contains file descriptor value
cmp    rax,-1H                      ; on failure returns -1
je    ERROR                         ; if file is successfull opened rax is +ve else it is -ve
                       
file 1,1,openmsg, openmsg_len
jmp next1

ERROR:
file 1,1, errormsg, errormsg_len
jmp EXIT

next1:
file 0,[fdisplay] ,buffer,200           ; reading contents of file in buffer
                                                    ; rax contains actual number of bytes read
 mov qword[bufferlen],rax            ; Total number of passes
 mov qword[cnt1],rax                   ; number of comparisons within a single pass
 mov qword[cnt3],rax                   ; total number of elements to be sorted
 dec byte[bufferlen];

BUBBLE:
mov al, byte[bufferlen];
mov byte[cnt1],al

mov rsi,buffer
mov rdi,buffer+1

loop:
mov bl,byte[rsi]
mov cl,byte[rdi]
 cmp bl,cl
 ja SWAP
 inc rsi
 inc rdi
 dec byte[cnt1]
 jnz loop
           
dec byte[bufferlen]
jnz BUBBLE
jmp END

SWAP:

 mov byte[rsi],cl
 mov byte[rdi],bl
 inc rsi
 inc rdi
 dec byte[cnt1]
 jnz loop
dec byte[bufferlen]
jnz BUBBLE

END:
file 1,1, sortmsg,sortmsg_len                           ;writing onto output screen
file 1,1, buffer,qword[cnt3]

file 1,qword[fdisplay],sortmsg,sortmsg_len      ;writing to input.txt
file 1,qword[fdisplay],buffer,qword[cnt3]           ;writing to input.txt

;Closing file
mov rax,3
mov rdi,filename
syscall

file 1,1, closemsg,closemsg_len

EXIT:
 mov rax,60
 mov rdi,0
 syscall




input.txt
5
4
3
2
1

Output:
[root@localhost ~]# nasm -f elf64 b_sort2.asm
[root@localhost ~]# ld -o s b_sort2.o
[root@localhost ~]# ./s
Enter file name
input.txt

File Opened Successfully
After Sorting
12345
File Closed Successfully[root@localhost ~]#

input.txt file content
5
4
3
2
1
After Sorting
12345



Monday, January 28, 2019

Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use add and shift method.(use of 64-bit registers is expected)




Flowchart of Multiplication Algorithm

     Example:
                 Using 4-bit numbers, perform the multiplication 9 × 12 (1001 × 1100).

                        
Step
Ax
Dx
Bx
Operation
0
0000 0000
1100
0000 1001
Initialization
1
0000 0000
0000 0000

1100
0110

0001 0010
0001 0010
Shift left B
Shift right Q
2
0000 0000
0000 0000

0110
0011
0010 0100
0010 0100
Shift left B
Shift right Q
3
0010 0100
0010 0100
0010 0100

0011
 0011
0001

0010 0100
0100 1000 0100 1000
Add B to A
Shift left B
Shift right Q
4
0110 1100
0110 1100
0110 1100

0001
 0001
 0000

0100 1000
1001 0000
1001 0000

Add B to A
 Shift left B
Shift right Q
                                                    



64-bit nasm code

section .data
            innum1 db "Enter First 8 bit HEX no: ",10
            lennum1 equ $ -innum1
            innum2 db "Enter Second 8 bit HEX no: ",10
            lennum2 equ $ -innum2
            product db "Multiplication of two HEX no is: ",10
            productlen equ $ -product

section .bss
            num1 resb 3
            num2 resb 3
            result resb 4

%macro output 2                  ;macro for output
            mov rax,01h
            mov rdi,01h
            mov rsi,%1
            mov rdx,%2
            syscall
%endmacro

%macro input 2                    ;macro for input
            mov rax,00h
            mov rdi,00h
            mov rsi,%1
            mov rdx,%2
            syscall
%endmacro

section .text
global _start
 _start:
            output innum1,lennum1
            input num1,03
            output innum2,lennum2
            input num2,03
           
            mov rsi,num1
           call AtoH
           mov bx,ax

            mov rsi,num2
           call AtoH
           mov dx,ax
; Actual logic for multiplication of two numbers using add and shift method
                                                                
            xor ax,ax
            mov ch,08
again:
            bt dx,0   
            jnc skip1   
           add ax,bx   
skip1:   
           shl bx,1  
            shr dx,1
            dec ch
            jnz again

; logic to convert HEX to ASCII

            mov rsi,result
            mov ch,04
            mov cl,04
again1:
            rol ax,cl
            mov bl,al
            and bl,0fh
            cmp bl,09h
            jng skip2
            add bl,07h
skip2:
            add bl,30h
            mov [rsi],bl
            inc rsi
            dec ch
            jnz again1

            output product, productlen
            output result,04
mov rax,3ch
mov rdi,00
syscall

;procedure to convert ASCII to HEX

AtoH:
            xor ax,ax
            mov cl,04
            mov ch,02
up:
            cmp byte[rsi],39h
            jng down
            sub byte[rsi],07h
down:
            sub byte[rsi],30h
            shl ax,cl
            add al,[rsi]
            inc rsi
            dec ch
            jnz up
ret

Output:
[root@localhost ~]# nasm -f elf64 mul.asm
[root@localhost ~]# ld -o mul mul.o
[root@localhost ~]# ./mul
Enter First 8 bit HEX no: 15
Enter Second 8 bit HEX no: 12
Multiplication of two HEX no is: 017A