Friday, May 21, 2021

Write an Assembly Language Program (ALP) to find the largest of given byte / Word / Double-word / 64-bit numbers.

 %macro scall 4 ;macro declaration with 4 parameters

        mov eax,%1                          ;1st parameter has been moved to eax
        mov ebx,%2                          ;2nd parameter has been moved to ebx
        mov ecx,%3                          ;3rd parameter has been moved to ecx
        mov edx,%4                          ;4th parameter has been moved to edx
        int 80h                             ;Call the Kernel
%endmacro                                   ;end of macro

section .data                                   ;.data segment begins here
        m1 db "Enter size of array: ",10d,13d   ;m1 initialised with a string
        l1 equ $-m1                             ;l1 stores length of m1 string
        m2 db "Enter array elements: ",10d,13d  ;m2 initialised with a string
        l2 equ $-m2                             ;l2 stores length of m2 string
        m3 db 10d,13d,"Largest: "               ;m3 initialised with a string
        l3 equ $-m3                             ;l3 stores length of m3 string
        m4 db 10d,13d                           ;m4 initialised with a string
        l4 equ $-m4                             ;l4 stores length of m4 string

section .bss                                    ;.bss segment starts here
        cnt resb 3                              ;variable with 3 byte size
        arr resb 3                              ;variable with 3 byte size
        cnt1 resb 3                             ;variable with 3 byte size
        arr1 resb 50                            ;variable with 50 byte size
        temp resb 2                             ;variable with 2 byte size
        char_ans resb 2                         ;variable with 2 byte size
	
section .text                                   ;.text segment starts here
        global _start                           ;declaring _start label as global
_start:                                         ;_start label
	
        scall 4,1,m1,l1                         ;macro call to display m1
        scall 3,0,arr,3                         ;macro call to input in arr

        mov esi,arr                             ;esi points to arr
        call asciihextohex                      ;calling procedure to convert into hex nos

        mov byte[cnt],dl                        ;moving values of dl into cnt
        mov byte[cnt1],dl                       ;moving values of dl into cnt
        scall 4,1,m2,l2                         ;macro call to display m2
        mov edi,arr1                            ;edi points to arr1
	
back:   scall 3,0,arr,3                         ;macro call to input in arr
        mov esi,arr                             ;esi points to arr
        call asciihextohex                      ;calling procedure to convert into hex nos

        mov [edi],dl                            ;move contents of dl at address of edi
        inc edi                                 ;increment edi to point to next element
        dec byte[cnt]                           ;decrement cnt variable
        jnz back                                ;jump if cnt is not zero to back label

        mov esi,arr1                            ;esi points to arr1
        mov al,[esi]                            ;move contents at esi into al
        inc esi                                 ;increment esi to point to next element
	
up1:    mov bl,[esi]                            ;move contents at esi into bl
        cmp al,bl                               ;compare al with bl
        jg next1                                ;if al is greater, jump to next1
        mov byte[temp],al                       ;copying al into temp
        mov al,bl                               ;copying bl into al
        mov bl,byte[temp]                       ;copying temp into bl
next1:  inc esi                                 ;increment esi
        dec byte[cnt1]                          ;decrement cnt1
        jnz up1                                 ;jump to up1, if cnt1 not zero
	
        mov ecx,02                              ;copy 02 into ecx
        mov esi,char_ans                        ;esi points to char_ans
	
up4:    rol al,4                                ;roll contents of al by 4 bits
        mov dl,al                               ;copy al into dl
        and dl,0FH                              ;AND dl with 0Fh
        cmp dl,09h                              ;compare dl with 09h
        jbe next2                               ;jump to next2, if dl is below or equal
        add dl,07h                              ;add 07h to dl
next2:  add dl,30h                              ;add 30h to dl
        mov[esi],dl                             ;move dl at esi address
        inc esi                                 ;increment esi
        dec ecx                                 ;decrement ecx
        jnz up4                                 ;jump to up4, if ecx not zero
	
        scall 4,1,m3,l3                         ;macro call to display m3
        scall 4,1,char_ans,2                    ;macro call to display char_ans
        scall 4,1,m4,l4                         ;macro call to display m4

        mov eax,1                               ;sys_Exit
        mov ebx,0                               ;sucessfull termination
        int 80h                                 ;call the kernel


asciihextohex:                                  ;procedure 
mov ecx,2                                       ;copy 2 into ecx
mov dl,0                                        ;copy 0 into dl

top:    rol dl,4                                ;roll contents of dl by 4 bits
        mov al,[esi]                            ;copy esi contents into al
        cmp al,39h                              ;compare al with 39h
        jbe down                                ;jump to down, if al is below or equal
        sub al,07h                              ;subtract 07h
down:   sub al,30h                              ;subtract 30h
        add dl,al                               ;add al with dl
        inc esi                                 ;increment esi
        loop top                                ;jump if ecx not equal to zero,decrement ecx
ret                                             ;return to calling address

Write a switch case driven Assembly Language Program (ALP) to perform 64-bit hexadecimal arithmetic operations (+,-,*, /) using suitable macros. Define procedure for each operation.

 %macro scall 4

        mov eax,%1
        mov ebx,%2
        mov ecx,%3
        mov edx,%4
        int 80h
%endmacro

section .data
        arr dq 0000003h,00000002h
        n equ 2

        menu db 10d,13d,"**********MENU**********"
             db 10d,13d,"1. Addition"
             db 10d,13d,"2. Subtraction"
             db 10d,13d,"3. Multiplication"
             db 10d,13d,"4. Division"
             db 10d,13d,"5. Exit"
             db 10d,13d,"Enter your Choice: "
        menu_len equ $-menu

        m1 db 10d,13d,"Addition: "
        l1 equ $-m1
        m2 db 10d,13d,"Substraction: "
        l2 equ $-m2
        m3 db 10d,13d,"Multiplication: "
        l3 equ $-m3
        m4 db 10d,13d,"Division: "
        l4 equ $-m4
		
	
section .bss
        answer resb 8		;to store the result of operation
        choice resb 2

section .text
        global _start:
        _start:

        up: 	scall 4,1,menu,menu_len
	        scall 3,0,choice,2	
	
        cmp byte[choice],'1'
        je case1
        cmp byte[choice],'2'
        je case2
        cmp byte[choice],'3'
        je case3
        cmp byte[choice],'4'
        je case4
        cmp byte[choice],'5'
        je case5

	
	
        case1:	scall 4,1,m1,l1
                call addition
                jmp up	
		
        case2: 	scall 4,1,m2,l2
	        call substraction
	        jmp up

        case3:	scall 4,1,m3,l3
	        call multiplication
	        jmp up

        case4:	scall 4,1,m4,l4
	        call division
	        jmp up
	
        case5:  mov eax,1
	        mov ebx,0
                int 80h	
		
		
	
;procedures for arithmetic and logical operations	
addition:
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up1:    add esi,8
        mov ebx,[esi]
        add eax,ebx
        loop up1	
        call display
ret
	
	
substraction:
	
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up2:    add esi,8
        mov ebx,[esi]
        sub eax,ebx
        loop up2	
        call display	
ret
		
		
multiplication:
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up3:    add esi,8
        mov ebx,[esi]
        mul ebx
        loop up3	
        call display
ret	

division:
        mov ecx,n
        dec ecx
	
        mov esi,arr
        mov eax,[esi]
up4:    add esi,8
        mov ebx,[esi]
        mov edx,0
        div ebx
        loop up4	
        call display
ret

or:
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up6:    add esi,8
        mov ebx,[esi]
        or eax,ebx
        loop up6	
        call display
ret

xor:
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up7:    add esi,8
        mov ebx,[esi]
        xor eax,ebx
        loop up7	
        call display
ret

and:
        mov ecx,n
        dec ecx	
        mov esi,arr
        mov eax,[esi]
up8:    add esi,8
        mov ebx,[esi]
        and eax,ebx
        loop up8
        call display
ret


display:
        mov esi,answer+7
        mov ecx,8

cnt:    mov edx,0
        mov ebx,16
        div ebx
        cmp dl,09h
        jbe add30
        add dl,07h
add30:  add dl,30h
        mov [esi],dl
        dec esi
        dec ecx
        jnz cnt
        scall 4,1,answer,8
ret

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

MAIN.asm

%macro IO 4  ; simple macro ,

mov rax,%1   ;  param 1 ->  system call number                             

mov rdi,%2    ;  param 2 ->  file descriptor                              

mov rsi,%3     ;  param 3 ->  message                       

mov rdx,%4      ;  param 4 ->  length                         

syscall                                     

%endmacro  

section .data

    m1 db 10,13, "enter string"   ;10 ->line feed 

    l1 equ $-m1                         

    m2 db 10,13, "Entered"    

    l2 equ $-m2                         

    m3 db 10,13,"Length is "     

    l3 equ $-m3 

    m4 db 10,13, "Write an X86/64 ALP to accept a string and to display its length"

    l4 equ $-m4

    m5 db 10,13, "Exiting now"  

    l5 equ $-m5

    

    m7 db 10    

section .bss

    string resb 50                      ;string array of size 50

    strl equ $-string 

    count resb 1

    _output resb 20

section .text

    global _start


_start:

   IO 1,1,m4,l4    ; display

    IO 1,1,m1,l1    ; display

 input:

    IO 0,0,string,strl

    mov [count],rax ;count now points to rax 

 output:

    IO 1,1,m2,l2    ; display

    IO 1,1,string,strl

    IO 1,1,m3,l3    ; display 

    mov rax,[count] ; value of count passed into rax   

    call hex_to_dec

    IO 1,1,_output,16

    IO 1,1,m7,1

 exit:

    IO 1,1,m5,l5     

    mov rax, 60     ; system call for exit

    mov rdi, 0      ; exit code 0

    syscall


hex_to_dec:

        mov rsi,_output+15  ; max size of display , for convinience set to 16 and rsipoints to output[16]

        mov rcx,2           ; loop count , or number of digits displayed , 2 digits (unlikely we will enter string > 99)(prints preceding zeros otherwise)

    letter2:

        xor rdx,rdx         ; setting rdx to null without setting a null byte (a tip i saw on reddit) needed to clean dl for use 

        mov rbx,10          ; conversion base

        div rbx             ; dividing by conversion base

        cmp dl,09h          ; comparing 09h with dl , since the division remainder ends up in dx and since its one digits its in dl

        jbe add30           ; if value under in 0-9 , we directly add 30h to get ascii 0-9

    add30:

        add dl,30h          ; adding 30h

        mov [rsi],dl        ; moving the ascii we generated to rsi

        dec rsi             ; rsi now points to the next place in display or output[n-1]

        dec rcx             ; loop 

        jnz letter2

ret


OUTPUT:

Write an X86/64 ALP to accept a string and to display its length
enter string
Entered
Rahul
Length is 
5
Exiting now

[Program exited with exit code 0]

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.

MAIN.asm


%macro IO 4

mov rax,%1

mov rdi,%2

mov rsi,%3

mov rdx,%4

syscall

%endmacro

section .data

m1 db 10,13,"Enter the five 64 bit numbers:"

l1 equ $-m1                         

m2 db 10,13,"The  five 64 bit numbers are:"   

l2 equ $-m2 

m4 db 10,13, "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."  

l4 equ $-m4

m5 db 10,13,"Exiting now"   

l5 equ $-m5

m6 db 10,13,"incorrect input error"

l6 equ $-m6

m7 db 10

debug db "debug " 

debug_l equ $-debug

time equ 5

size equ 8

section .bss

arr resb 300

_input resb 20

_output resb 20

count resb 1

section .text

global _start

_start:

IO 1,1,m4,l4

    mov byte[count],time        ; store time = 5 in count;

mov rbp,arr                    ;rbp points to begining of arr

IO 1,1,m1,l1

input:

    IO 0,0,_input,17

    IO 1,1,debug,debug_l

    IO 1,1,_input,17

    

call ascii_to_hex

        mov [rbp],rbx   ; put the complete summed rbx value to arr[n] 

add rbp,size    ; move to next value of array 8 -> 4*2 = 1 place -> arr[n+1]

dec byte[count] ; loop

jnz input


mov byte[count],time ; set loop count to 5 

mov rbp,arr     ;make rbp point to arr beginning

jmp display

display:

    mov rax,[rbp]   ; address of rbp in rax 

call hex_to_ascii

IO 1,1,m7,1

IO 1,1,_output,16

add rbp,size    ; move to next value of array 8 -> 4*2 = 1 place arr[n+1]

dec byte[count] ; loop

jnz display

jmp exit

exit:

IO 1,1,m5,l5

mov rax,60

mov rdi,0

syscall

error:

    IO 1,1,m6,l6

    jmp exit

ascii_to_hex:

    mov rsi,_input

    mov rcx,16

    xor rbx,rbx  ;cleaning rbx since rbx == rbx , rbx is set to 0without wasting the space

        xor rax,rax  ;cleaning rax 

    letter:

        rol rbx,4    ; shifting rbx to left by 4 bytes 

    mov al,[rsi] ; adrress of rsi (_input ) in al _input[0]

    cmp al,47h   ; error checking

    jge error    ; 

    cmp al,39h   ;if < ascii 39 => 0-9

    jbe skip     

    sub al,07h   ;else => ascii is (A-F)

    skip:

        sub al,30h   ; get value between 0-9

    add rbx,rax  ; add generated hex value to rbx 

    inc rsi      ; now rsi points at _input[n+1]

    dec rcx      ; loop

    jnz letter

    ret

hex_to_ascii:

        mov rsi,_output+15   ;max display of 16 characters and rsi points to _output[16]

        mov rcx,16           ;loop runs 16 times 

    letter2:

        xor rdx,rdx          ;cleaning rdx need dl for division remainder and  

        mov rbx,16           ;base 16  

        div rbx              ;dividing by base 16

        cmp dl,09h           ;checking if hex value < 9  

        jbe add30            ;if yes simply add 30h to get the ascii 

        add dl,07h           ;else => (A-F)  so add 7 to make it 37 total 

    add30:

        add dl,30h           ;common step of adding 30h 

        mov [rsi],dl         ;move generated ascii to  _output[n] 

        dec rsi              ;rsi points to _output[n-1]

        dec rcx              ;loop    

        jnz letter2

ret



OUTPUT:

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.
Enter the five 64 bit numbers:
debug 55555555A5555555
debug 44444444F4444434
debug 33333D3333333333
debug 2222222222E22222
debug 1111111111111111 
55555555A5555555
44444444F4444434
33333D3333333333
2222222222E22222
1111111111111111
Exiting now

Write an Assembly Language Program (ALP) to find the largest of given byte / Word / Double-word / 64-bit numbers.

  %macro scall 4 ;macro declaration with 4 parameters          mov eax , %1 ;1st p...