Thursday, March 23, 2017

Write 80387 ALP to find the roots of the quadratic equation. All the possible cases must be considered in calculating the roots.

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.07

Problem Statement:Write 80387 ALP to find the roots of the quadratic equation. All the possible cases must be considered in calculating the roots.

section .data
msg1 db "Complex Root",10
msglen1 equ $-msg1
msg2 db "Root1: "
msglen2 equ $-msg2
msg3 db "Root2: "
msglen3 equ $-msg3
a dd 1.00
b dd 8.00
c dd 15.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
  
  
  fstsw ax                ;Stores the coprocessor status word ;into either a word in memory or the AX register
  sahf        ;Stores the AH register into the FLAGS register.
  jb no_real_solutions         ; if disc < 0, 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 edi,temp                ;mov dnum address into edi
        mov ecx,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 [edi],dl        ;mov dl into dnum
                inc edi            ;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

Tuesday, March 21, 2017

Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation.

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.12

Problem Statement:Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation.

section .data

msg1 db 10, 'mean is:  '
msg1len equ $- msg1

msg2 db 10, 'std deviation is:'
msg2len equ $- msg2

msg3 db 10, 'variance is:'
msg3len equ $- msg3

data dd 9.0,1.0
datacnt dw 02
hdec dq 100

decpt db '.'

section .bss
 

res rest 01
mean resd 01
var resd 01
dispbuff resb 01

%macro disp 2
    mov eax,04
    mov ebx,01
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro
%macro accept 2
    mov eax,03
    mov ebx,00
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro

section .text
global _start
_start:

disp msg1,msg1len
  
       finit
    fldz
    mov rbx,data
    mov rsi,00
    xor rcx,rcx
    mov cx,[datacnt]



bk:    fadd dword [rbx+rsi*4]
    inc rsi
    loop bk

    fidiv word[datacnt]
    fst dword[mean]
      
    call dispres

    MOV RCX,00
    MOV CX,[datacnt]
    MOV RBX,data
    MOV RSI,00
    FLDZ
up1:    FLDZ
    FLD DWORD[RBX+RSI*4]
    FSUB DWORD[mean]
    FST ST1
    FMUL
    FADD
    INC RSI
    LOOP up1
    FIDIV word[datacnt]
    FST dWORD[var]
    FSQRT

    disp msg2,msg2len
    CALL dispres

    FLD dWORD[var]
    disp msg3,msg3len
    CALL dispres
  
  
exit:     mov eax,01
    mov ebx,00
    int 80h

disp8_proc:
    mov rdi,dispbuff
    mov rcx,02
back:    rol bl,04
    mov dl,bl
    and dl,0FH
    cmp dl,09
    jbe next1
    add dl,07H
next1:  add dl,30H
    mov [rdi],dl
    inc rdi
    loop back
    ret
  
dispres:
    fimul dword[hdec]
    fbstp tword[res]
    xor rcx,rcx
    mov rcx,09H
    mov rsi,res+9
up2:    push rcx
    push rsi
    mov bl,[rsi]
    call disp8_proc
    disp dispbuff,2  
    pop rsi
    dec rsi
    pop rcx
    loop up2
    disp decpt,1

    mov bl,[res]
    call disp8_proc
    disp dispbuff,2  
    ret

output:

student@student-OptiPlex-390:~$ nasm -f elf64 mean.nasm
student@student-OptiPlex-390:~$ ld -o mean  mean.o
student@student-OptiPlex-390:~$ ./mean

mean is:  000000000000000005.00
std deviation is:000000000000000004.00
variance is:000000000000000016.00
student@student-OptiPlex-390:~$ ^C
student@student-OptiPlex-390:~$






Write X86 menu driven Assembly Language Program (ALP) to implement OS (DOS) commands TYPE, COPY and DELETE using file operations. User is supposed to provide command line arguments in all cases.

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.08

Problem Statement:Write X86 menu driven Assembly Language Program (ALP) to implement OS (DOS) commands TYPE, COPY and DELETE using file operations. User is supposed to provide
command line arguments in all cases.


%macro cmn 4            ;input/output
    mov rax,%1
    mov rdi,%2
    mov rsi,%3
    mov rdx,%4
    syscall
%endmacro
%macro exit 0
    mov rax,60
    mov rdi,0
    syscall
%endmacro

%macro fopen 1
    mov    rax,2        ;open
    mov    rdi,%1    ;filename
    mov    rsi,2        ;mode RW
    mov    rdx,0777o    ;File permissions
    syscall
%endmacro

%macro fread 3
    mov    rax,0        ;read
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fwrite 3
    mov    rax,1        ;write/print
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fclose 1
    mov    rax,3        ;close
    mov    rdi,%1    ;file handle
    syscall
%endmacro

section .data
    menu db 'MENU : ',0Ah
        db "1. TYPE",0Ah
        db "2. COPY",0Ah
        db "3. DELETE",0Ah
        db "4. Exit",0Ah
        db "Enter your choice : "  
    menulen equ $-menu
    msg db "Command : "
    msglen equ $-msg
    cpysc db "File copied successfully !!",0Ah
    cpysclen equ $-cpysc
    delsc db 'File deleted successfully !!',0Ah
    delsclen equ $-delsc
    err db "Error ...",0Ah
    errlen equ $-err
    cpywr db 'Command does not exist',0Ah
    cpywrlen equ $-cpywr
    err_par db 'Insufficient parameter',0Ah
    err_parlen equ $-err_par
  

section .bss
    choice resb 2
    buffer resb 50
    name1 resb 15
    name2 resb 15
    cmdlen resb 1
    filehandle1 resq 1
    filehandle2 resq 1
  
    abuf_len        resq    1        ; actual buffer length
    dispnum resb 2

    buf resb    4096
    buf_len equ $-buf        ; buffer initial length

section .text
global _start
_start:

again:    cmn 1,1,menu,menulen
    cmn 0,0,choice,2
  
    mov al,byte[choice]
    cmp al,31h
    jbe op1
    cmp al,32h
    jbe op2
    cmp al,33h
    jbe op3
  
        exit  
        ret

op1:
    call tproc
    jmp again

op2:
    call cpproc
    jmp again


op3:
    call delproc
    jmp again



;type command procedure
tproc:
    cmn 1,1,msg,msglen
    cmn 0,0,buffer,50
    mov byte[cmdlen],al
    dec byte[cmdlen]

    mov rsi,buffer          
    mov al,[rsi]            ;search for correct type command
    cmp al,'t'
    jne skipt
    inc rsi
    dec byte[cmdlen]
    jz skipt
    mov al,[rsi]
    cmp al,'y'
    jne skipt
    inc rsi
    dec byte[cmdlen]
    jz skipt
    mov al,[rsi]
    cmp al,'p'
    jne skipt
    inc rsi
    dec byte[cmdlen]
    jz skipt
    mov al,[rsi]
    cmp al,'e'
    jne skipt
    inc rsi
    dec byte[cmdlen]  
    jnz correctt
    cmn 1,1,err_par,err_parlen
    call exit
  
skipt:    cmn 1,1,cpywr,cpywrlen
    exit
correctt:  
    mov rdi,name1            ;finding file name
    call find_name

    fopen name1            ; on succes returns handle
    cmp rax,-1H            ; on failure returns -1
    jle error
    mov [filehandle1],rax  

    xor rax,rax
    fread [filehandle1],buf, buf_len
    mov [abuf_len],rax
    dec byte[abuf_len]

    cmn 1,1,buf,abuf_len        ;printing file content on screen

ret


;copy command procedure
cpproc:
    cmn 1,1,msg,msglen
    cmn 0,0,buffer,50        ;accept command
    mov byte[cmdlen],al
    dec byte[cmdlen]

    mov rsi,buffer          
    mov al,[rsi]            ;search for copy
    cmp al,'c'
    jne skip
    inc rsi
    dec byte[cmdlen]
    jz skip
    mov al,[rsi]
    cmp al,'o'
    jne skip
    inc rsi
    dec byte[cmdlen]
    jz skip
    mov al,[rsi]
    cmp al,'p'
    jne skip
    inc rsi
    dec byte[cmdlen]
    jz skip
    mov al,[rsi]
    cmp al,'y'
    jne skip
    inc rsi
    dec byte[cmdlen]  
    jnz correct
    cmn 1,1,err_par,err_parlen
    exit
  
skip:    cmn 1,1,cpywr,cpywrlen
    exit
correct:  
    mov rdi,name1            ;finding first file name
    call find_name

    mov rdi,name2            ;finding second file name
    call find_name

skip3:    fopen name1            ; on succes returns handle
    cmp rax,-1H            ; on failure returns -1
    jle error
    mov [filehandle1],rax  

    fopen name2            ; on succes returns handle
    cmp rax,-1H            ; on failure returns -1
    jle error
    mov [filehandle2],rax
  
    xor rax,rax
    fread [filehandle1],buf, buf_len
    mov [abuf_len],rax
    dec byte[abuf_len]
  
    fwrite [filehandle2],buf, [abuf_len]        ;write to file
  
    fclose    [filehandle1]    
    fclose    [filehandle2]
    cmn 1,1,cpysc,cpysclen
  
    jmp again
error:
    cmn 1,1,err,errlen
    exit
ret



;delete command procedure
delproc:
  
    cmn 1,1,msg,msglen
    cmn 0,0,buffer,50        ;accept command
    mov byte[cmdlen],al
    dec byte[cmdlen]
  
    mov rsi,buffer          
    mov al,[rsi]            ;search for copy
    cmp al,'d'
    jne skipr
    inc rsi
    dec byte[cmdlen]
    jz skipr
    mov al,[rsi]
    cmp al,'e'
    jne skipr
    inc rsi
    dec byte[cmdlen]
    jz skipr
    mov al,[rsi]
    cmp al,'l'
    jne skipr
    inc rsi
    dec byte[cmdlen]
    jnz correctr
    cmn 1,1,err_par,err_parlen
    exit
  
skipr:    cmn 1,1,cpywr,cpywrlen
    exit

correctr:  
    mov rdi,name1            ;finding first file name
    call find_name
  
    mov rax,87            ;unlink system call
    mov rdi,name1
    syscall
  
    cmp rax,-1H            ; on failure returns -1
    jle errord
    cmn 1,1,delsc,delsclen
    jmp again

errord:
    cmn 1,1,err,errlen
    exit

ret


find_name:                ;finding file name from command
    inc rsi
    dec byte[cmdlen]
cont1:    mov al,[rsi]
    mov [rdi],al
    inc rdi
    inc rsi
    mov al,[rsi]
    cmp al,20h            ;searching for space
    je skip2
    cmp al,0Ah            ;searching for enter key
    je skip2
    dec byte[cmdlen]
    jnz cont1
    cmn 1,1,err,errlen
    exit
  
skip2:
ret

Tuesday, March 14, 2017

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

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.07

Problem Statement: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 


A7_bsort.asm

;************************************************************

%include    "macro.asm"
;************************************************************
section .data
    nline        db    10
    nline_len    equ    $-nline

    ano        db     10,10,10,10,"ML assignment 07 :- Bubble sort using file operations"
            db           10,"---------------------------------------------------",10
    ano_len    equ    $-ano


    filemsg    db    10,"Enter filename of input data    : "
    filemsg_len    equ    $-filemsg  

    omsg        db    10,"Sorting using bubble sort Operation successful."
            db    10,"Output stored in same file...",10,10
    omsg_len    equ    $-omsg
 
    errmsg    db    10,"ERROR in opening/reading/writing File...",10
    errmsg_len    equ    $-errmsg

    ermsg        db    10,"ERROR in writing File...",10
    ermsg_len    equ    $-ermsg

    exitmsg    db    10,10,"Exit from program...",10,10
    exitmsg_len    equ    $-exitmsg

;************************************************************
section .bss
    buf            resb    1024
    buf_len        equ    $-buf        ; buffer length

    filename        resb    50  

    filehandle        resq    1
    abuf_len        resq    1        ; actual buffer length

    array            resb    10
    n            resq    1
;************************************************************
section .text
    global _start
      
_start:
        display    ano,ano_len                ;assignment no.

        display    filemsg,filemsg_len      
        accept     filename,50
        dec    rax
        mov    byte[filename + rax],0           ; blank char/null char

        fopen    filename            ; on succes returns handle
        cmp    rax,-1H                ; on failure returns -1
        je    Error
        mov    [filehandle],rax  

        fread    [filehandle],buf, buf_len
        dec    rax                    ; EOF
        mov    [abuf_len],rax

        call    bsort_proc

        jmp    Exit

Error:    display    errmsg, errmsg_len

Exit:        display    exitmsg,exitmsg_len
      
  
    mov    rax,60    ;exit
    mov    rdi,0
    syscall
;************************************************************
bsort_proc:                        ; Bubble sort procedure
        call    buf_array_proc

        mov    rax,0
        mov    rbp,[n]
        dec    rbp

        mov    rcx,0
        mov    rdx,0
        mov    rsi,0
        mov    rdi,0

        mov    rcx,0                ; i=0

oloop:        mov    rbx,0                ; j=0

        mov    rsi,array            ; a[j]

iloop:        mov    rdi,rsi                ; a[j+1]
        inc    rdi

        mov    al,[rsi]
        cmp    al,[rdi]
        jbe    next

        mov    dl,0
        mov    dl,[rdi]            ; swap
        mov    [rdi],al
        mov    [rsi],dl

next:        inc    rsi
        inc    rbx                ; j++
        cmp    rbx,rbp
        jb    iloop
      
        inc    rcx
        cmp    rcx,rbp
        jb    oloop

    fwrite    [filehandle],omsg, omsg_len
    fwrite    [filehandle],array,[n]

    fclose [filehandle]  

    display    omsg, omsg_len
    display    array,[n]  

    RET

Error1:
    display    ermsg, ermsg_len
    RET
;************************************************************
buf_array_proc:

    mov    rcx,0
    mov    rsi,0
    mov    rdi,0

    mov    rcx,[abuf_len]
    mov    rsi,buf
    mov    rdi,array

next_num:
    mov    al,[rsi]
    mov    [rdi],al

    inc    rsi        ; number
    inc    rsi        ; newline
    inc    rdi

    inc    byte[n]        ; counter
  
    dec    rcx        ; number
    dec    rcx        ; newline
    jnz    next_num
    ret
;**********************************************************

macro.asm


;**********************************************************
;macro.asm
;macros as per 64 bit conventions

%macro accept 2
    mov    rax,0        ;read
    mov    rdi,0        ;stdin/keyboard
    mov    rsi,%1    ;buf
    mov    rdx,%2    ;buf_len
    syscall
%endmacro

%macro display 2
    mov    rax,1        ;print
    mov    rdi,1        ;stdout/screen
    mov    rsi,%1    ;msg
    mov    rdx,%2    ;msg_len
    syscall
%endmacro

%macro fopen 1
    mov    rax,2        ;2 for open file
    mov    rdi,%1            ;filename
    mov    rsi,2        ;mode RW
    mov    rdx,0777o    ;File permissions
    syscall
%endmacro

%macro fread 3
    mov    rax,0    ;read
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fwrite 3
    mov    rax,1    ;write/print
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fclose 1
    mov    rax,3     ;close
    mov    rdi,%1     ;file handle
    syscall
%endmacro

;**********************************************************


data.txt 

 
4
5
1
2
3
;**********************************************************

after execution data.txt is:

;**********************************************************
 
4
5
1
2
3


Sorting using bubble sort Operation successful.
Output stored in same file...

12345 

;**********************************************************
Note:All files are save within single folder. 

Write X86 ALP to find, a) Number of Blank spaces b) Number of lines c) Occurrence of a particular character. Accept the data from the text file. The text file has to be accessed during Program_1 execution and write FAR PROCEDURES in Program_2 for the rest of the processing. Use of PUBLIC and EXTERN directives is mandatory.

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.05

Problem Statement:Write X86 ALP to find, a) Number of Blank spaces b) Number of lines c) Occurrence of a particular character. Accept the data from the text file. The text file has to be accessed during Program_1 execution and write FAR PROCEDURES in Program_2 for the rest of the processing. Use of PUBLIC and EXTERN directives is mandatory. 


A5_file1.asm
 
;************************************************************

extern    far_proc            ; [ FAR PROCRDURE
                    ;   USING EXTERN DIRECTIVE ]

global    filehandle, char, buf, abuf_len

%include    "macro.asm"

;************************************************************
section .data
    nline        db    10
    nline_len    equ    $-nline

    ano        db     10,10,10,10,"ML assignment 05 :- String Operation using Far Procedure"
            db           10,"---------------------------------------------------",10
    ano_len    equ    $-ano

    filemsg    db    10,"Enter filename for string operation    : "
    filemsg_len    equ    $-filemsg  
 
    charmsg    db    10,"Enter character to search    : "
    charmsg_len    equ    $-charmsg

    errmsg    db    10,"ERROR in opening File...",10
    errmsg_len    equ    $-errmsg

    exitmsg    db    10,10,"Exit from program...",10,10
    exitmsg_len    equ    $-exitmsg

;************************************************************
section .bss
    buf            resb    4096
    buf_len        equ    $-buf        ; buffer initial length

    filename        resb    50  
    char            resb    2  

    filehandle        resq    1
    abuf_len        resq    1        ; actual buffer length


;************************************************************
section .text
    global _start
      
_start:
        display    ano,ano_len        ;assignment no.

        display    filemsg,filemsg_len      
        accept     filename,50
        dec    rax
        mov    byte[filename + rax],0        ; blank char/null char

        display    charmsg,charmsg_len      
        accept     char,2
      
        fopen    filename            ; on succes returns handle
        cmp    rax,-1H            ; on failure returns -1
        jle    Error
        mov    [filehandle],rax  

        fread    [filehandle],buf, buf_len
        mov    [abuf_len],rax

        call    far_proc
        jmp    Exit

Error:    display    errmsg, errmsg_len

Exit:    display    exitmsg,exitmsg_len
  
    display nline,nline_len

    mov rax,60
    mov rdi,0
    syscall



A5_file2.asm

;************************************************************
global    far_proc      

extern    filehandle, char, buf, abuf_len

%include "macro.asm"
;************************************************************
section .data
    nline        db    10,10
    nline_len:    equ    $-nline

    smsg        db    10,"No. of spaces are    : "
    smsg_len:    equ    $-smsg
   
    nmsg        db    10,"No. of lines are    : "
    nmsg_len:    equ    $-nmsg

    cmsg        db    10,"No. of character occurances are    : "
    cmsg_len:    equ    $-cmsg


;************************************************************
section .bss

    scount    resq    1
    ncount    resq    1
    ccount    resq    1

    dispbuff    resb    4

;************************************************************
section .text
;    global    _main
;_main:

far_proc:                  ;FAR Procedure
   
        mov    rax,0
        mov    rbx,0
        mov    rcx,0
        mov    rsi,0  

        mov    bl,[char]
        mov    rsi,buf
        mov    rcx,[abuf_len]

again:    mov    al,[rsi]

case_s:    cmp    al,20h        ;space : 32 (20H)
        jne    case_n
        inc    qword[scount]
        jmp    next

case_n:    cmp    al,0Ah        ;newline : 10(0AH)
        jne    case_c
        inc    qword[ncount]
        jmp    next

case_c:    cmp    al,bl            ;character
        jne    next
        inc    qword[ccount]

next:        inc    rsi
        dec    rcx            ;
        jnz    again            ;loop again

        display smsg,smsg_len
        mov    rbx,[scount]
        call    display16_proc
   
        display nmsg,nmsg_len
        mov    rbx,[ncount]
        call    display16_proc

        display cmsg,cmsg_len
        mov    rbx,[ccount]
        call     display16_proc

    fclose    [filehandle]
    ret


;************************************************************
display16_proc:
    mov rdi,dispbuff    ;point esi to buffer
    mov rcx,4        ;load number of digits to display
dispup1:
    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 dispskip1        ;if less than 39h akip adding 07 more
    add dl,07h        ;else add 07

dispskip1:
    mov [rdi],dl        ;store ASCII code in buffer
    inc rdi            ;point to next byte
    loop dispup1        ;decrement the count of digits to display
                ;if not zero jump to repeat

    display dispbuff,4    ;
   
    ret
;************************************************************






macro.asm

;*************************************************************
;macro.asm
;macros as per 64 bit conventions

%macro accept 2
    mov    rax,0    ;read
    mov    rdi,0    ;stdin/keyboard
    mov    rsi,%1    ;buf
    mov    rdx,%2    ;buf_len
    syscall
%endmacro

%macro display 2
    mov    rax,1    ;print
    mov    rdi,1    ;stdout/screen
    mov    rsi,%1    ;msg
    mov    rdx,%2    ;msg_len
    syscall
%endmacro

%macro fopen 1
    mov    rax,2        ;open
    mov    rdi,%1        ;filename
    mov    rsi,2        ;mode RW
    mov    rdx,0777o    ;File permissions
    syscall
%endmacro

%macro fread 3
    mov    rax,0    ;read
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fwrite 3
    mov    rax,1    ;write/print
    mov    rdi,%1    ;filehandle
    mov    rsi,%2    ;buf
    mov    rdx,%3    ;buf_len
    syscall
%endmacro

%macro fclose 1
    mov    rax,3        ;close
    mov    rdi,%1    ;file handle
    syscall
%endmacro

;************************************************************ 
myfile.txt

"Welcome!!!"
Computer Engineering
Sinhgad Institute of Technology & Science Narhe,Pune


 ;*******************Output**********************************
;[root@localhost A5_Far]# nasm -f elf64 A5_file1.asm
;[root@localhost A5_Far]# nasm -f elf64 A5_file2.asm
;[root@localhost A5_Far]# ld -o A5_file1 A5_file1.o A5_file2.o
;[root@localhost A5_Far]# ./A5_file1




;ML assignment 05 :- String Operation using Far Procedure
;---------------------------------------------------

;Enter filename for string operation    : myfile.txt

;Enter character to search    : e

;No. of spaces are    : 0007
;No. of lines are    : 0003
;No. of character occurances are    : 000B

;Exit from program...


;[root@localhost A5_Far]#
;************************************************************


Note:All files are save within single folder.  

Write X86/64 ALP to switch from real mode to protected mode and display the values of GDTR, LDTR, IDTR, TR and MSW Registers.

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.06

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. 

;*****************************************************
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::"
gdtmsg_len equ $-gdtmsg

ldtmsg db 10,"LDT Contents are::"
ldtmsg_len equ $-ldtmsg

idtmsg db 10,"IDT Contents are::"
idtmsg_len equ $-idtmsg

trmsg db 10,"Task Register Contents are::"
trmsg_len equ $-trmsg

mswmsg db 10,"Machine Status Word::"
mswmsg_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

dispbuff resb 04

%macro display 2
    mov    rax,1    ;print
    mov    rdi,1    ;stdout/screen
    mov    rsi,%1    ;msg
    mov    rdx,%2    ;msg_len
    syscall
%endmacro

;*******************************************************

section .text
global _start
_start:
    smsw eax    ;Stores the machine status word (bits 0 

                ;through   15 of control register CR0) into eax.

    mov [cr0_data],eax

    bt eax,0    ;Checking PE(Protected Mode Enable) bit(LSB), 

                ;if 1=Protected Mode, else Real Mode
    jc prmode
    display rmodemsg,rmsg_len
    jmp nxt1

prmode: display pmodemsg,pmsg_len

nxt1:sgdt [gdt]    

     sldt [ldt]         
     sidt [idt]  
     str [tr]  
    ;.......display gdt data...........

     display gdtmsg,gdtmsg_len

     mov bx,[gdt+4]
     call display16_proc

     mov bx,[gdt+2]
     call display16_proc

     display colmsg,1

     mov bx,[gdt]
     call display16_proc
  
    ;......display ldt data...........
    
     display ldtmsg,ldtmsg_len
     mov bx,[ldt]
     call display16_proc

    ;......display idt data...........

    display idtmsg,idtmsg_len

    mov bx,[idt+4]
    call display16_proc

    mov bx,[idt+2]
    call display16_proc

    display colmsg,1

    mov bx,[idt]
    call display16_proc

    ;....display task register data..........

    display trmsg,trmsg_len

    mov bx,[tr]
    call display16_proc

    ;....display machine status word data......

    display mswmsg,mswmsg_len

    mov bx,[cr0_data+2]
    call display16_proc

    mov bx,[cr0_data]
    call display16_proc

    display nwline,1

    mov rax,60
    mov rdi,0
    syscall
;************************************************************
display16_proc:
    mov rdi,dispbuff    ;point esi to buffer
    mov rcx,4        ;load number of digits to display
dispup1:
    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 dispskip1        ;if less than 39h akip adding 07 more
    add dl,07h        ;else add 07

dispskip1:
    mov [rdi],dl        ;store ASCII code in buffer
    inc rdi            ;point to next byte
    loop dispup1        ;decrement the count of digits to display
                ;if not zero jump to repeat

    display dispbuff,4    ;
  
    ret
;******************Output*************************************

;[root@localhost MIT2016]# nasm -f elf64 proc.asm
;[root@localhost MIT2016]# ld -o proc proc.o
;[root@localhost MIT2016]# ./proc

;Processor is in Protected Mode
;GDT Contents are::B7304000:007F
;LDT Contents are::0000
;IDT Contents are::81BDD000:0FFF
;Task Register Contents are::0040
;Machine Status Word::8005FFFF
;[root@localhost MIT2016]#

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)

                                            Savitribai Phule Pune University
                            Second Year of Computer Engineering (2015 Course)
                                                     210257: Microprocessor Lab

                                                                Assignment No.04

Problem Statement: 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) 

;********************************************
;Assignment no:4A
;Title:Multiplication using successive addition
;*********************************************
section .data

welmsg db 10,'Multiplication using successive addition',10
welmsg_len equ $-welmsg

nummsg db 10,'Enter two digits of Number::'
nummsg_len equ $-nummsg

resmsg db 10,'Multiplication of elements::'
resmsg_len equ $-resmsg

blankmsg db 10,'',10
blank_len equ $-blankmsg

;**********.bss Section**********************

section .bss

    numascii resb 03
    num1 resb 02
    num2 resb 02
    result resb 01
    dispbuff resb 04

%macro display 2
    mov rax,01
    mov rdi,01
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro accept 2
    mov rax,00
    mov rdi,00
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

;**********.text Section**********************
section .text
global _start
_start:

    display welmsg,welmsg_len

    display nummsg,nummsg_len
    accept numascii,3
    call packnum
    mov byte[num1],bl

    display nummsg,nummsg_len
    accept numascii,3
    call packnum
    mov byte[num2],bl


    mov cx,[num2]
         mov edx,00h            ;Temporary Addition
    mov eax,[num1]
       

addup:    add edx,eax
    loop addup
   
    mov [result],edx

    display resmsg,resmsg_len
    mov ebx,[result]

    call disp16_proc

    display blankmsg,blank_len

exit:    mov rax,60
    mov rbx,00
    syscall

;**********Packnum Procedure**********************
packnum:
    mov bl,0
    mov ecx,02
    mov esi,numascii
    up1:rol bl,04
    mov al,[esi]
    cmp al,39h
    jbe skip1
    sub al,07h
 skip1: sub al,30h
    add bl,al
    inc esi
    loop up1
    ret
;**********Display Procedure**********************
disp16_proc:
    mov ecx,4
    mov edi,dispbuff
 dup1:  rol bx,4
    mov al,bl
    and al,0fh
    cmp al,09
    jbe dskip
    add al,07h
 dskip: add al,30h
        mov [edi],al
        inc edi
        loop dup1
        display dispbuff,4
        ret
;**********Output**********************

;[root@localhost MIT2016]# nasm -f elf64 Ass3A.asm
;[root@localhost MIT2016]# ld -o Ass3A Ass3A.o
;[root@localhost MIT2016]# ./Ass3A

;Multiplication using successive addition

;Enter two digits of Number::04

;Enter two digits of Number::05

;Multiplication of elements::0014

;[root@localhost MIT2016]#


;********************************************
;Assignment no:4B
;........................................
;Title:Multiplication using Add & Shift method
;*********************************************

section .data

welmsg db 10,'Multiplication using Add & Shift method',10
welmsg_len equ $-welmsg

nummsg db 10,'Enter two digits of Number::'
nummsg_len equ $-nummsg

resmsg db 10,'Multiplication of elements::'
resmsg_len equ $-resmsg

blankmsg db 10,'',10
blank_len equ $-blankmsg

;**********.bss Section**********************

section .bss

    numascii resb 03
    num1 resb 02
    num2 resb 02
    result resb 02
    dispbuff resb 04

%macro display 2
    mov rax,01
    mov rdi,01
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro accept 2
    mov rax,00
    mov rdi,00
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

;**********.text Section**********************
section .text
global _start
_start:

    display welmsg,welmsg_len

    display nummsg,nummsg_len
    accept numascii,3
    call packnum
    mov byte[num1],bl

    display nummsg,nummsg_len
    accept numascii,3
    call packnum
    mov byte[num2],bl

    mov al,[num1]

    mov cl,0
    mov edx,0
    mov edx,08h

    addup:
        rcr al,01
        jnc next1
        mov bh,00h
        shl bx,cl
        add [result],bx
        mov bl,[num2]
    next1:     inc cl
               dec edx   
        jnz addup

    display resmsg,resmsg_len
    mov ebx,[result]
    call disp16_proc

display blankmsg,blank_len

exit:    mov rax,60
    mov rbx,00
    syscall

;**********Packnum Procedure**********************
packnum:
    mov bl,0
    mov ecx,02
    mov esi,numascii
    up1:rol bl,04
    mov al,[esi]
    cmp al,39h
    jbe skip1
    sub al,07h
 skip1: sub al,30h
    add bl,al
    inc esi
    loop up1
    ret
;**********Display Procedure**********************
disp16_proc:
    mov ecx,4
    mov edi,dispbuff
 dup1:  rol bx,4
    mov al,bl
    and al,0fh
    cmp al,09
    jbe dskip
    add al,07h
 dskip: add al,30h
        mov [edi],al
        inc edi
        loop dup1
        display dispbuff,4
        ret
;**********Output**********************

;[root@localhost MIT2016]# nasm -f elf64 Ass3B.asm
;[root@localhost MIT2016]# ld -o Ass3B Ass3B.o
;[root@localhost MIT2016]# ./Ass3B

;Multiplication using Add & Shift method

;Enter two digits of Number::04

;Enter two digits of Number::05

;Multiplication of elements::0014

;[root@localhost MIT2016]#