This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Pages

This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Powered by Blogger.

5) ALP for Real to Protected Mode

;-------------------------------------------------------------------------------------------------------
     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
            nline                db        10,10
            nline_len:         equ      $-nline
            colon               db        ":"
            rmsg                db        10,'Processor is in Real Mode...'
            rmsg_len:         equ      $-rmsg
            pmsg                db        10,'Processor is in Protected Mode...'
            pmsg_len:        equ      $-pmsg
            gmsg                db        10,"GDTR (Global Descriptor Table Register)           : "
            gmsg_len:        equ      $-gmsg
            imsg                 db        10,"IDTR (Interrupt Descriptor Table Register)         : "
            imsg_len:         equ      $-imsg
            lmsg                 db        10,"LDTR (Local Descriptor Table Register) : "
            lmsg_len:         equ      $-lmsg
            tmsg                db        10,"TR (Task Register)                       : "
            tmsg_len:         equ      $-tmsg
            mmsg               db        10,"MSW (Machine Status Word)      : "
            mmsg_len:       equ      $-mmsg
;---------------------------------------------------------------------
Section   .bss
            GDTR             resw     3                      ; 3 words i.e. 48-bits
            IDTR               resw     3
            LDTR              resw     1                      ; 1 word i.e. 16-bits
            TR                   resw     1
            MSW               resw     1
            char_sum         resb      4                      ; 4 digits i.e. 16bits
;---------------------------------------------------------------------
;Macro declaration for display and exit
%macro  print   2
            mov     eax, 4
            mov     ebx, 1
            mov     ecx, %1
            mov     edx, %2
            int   80h
%endmacro
%macro           exit      0
            mov     eax, 1
            mov     ebx, 0
            int  80h
%endmacro
;---------------------------------------------------------------------
;For modifying 32-bit program check line by line to make all 'e' as 'r' and remaining modification
;for 64-bit no.


section    .text
            global   _start
_start:
            SMSW [MSW]
            mov     rax,[MSW]
            ror        rax,1                                                    ; Check PE bit, if 1=Protected Mode, else Real Mode
            jc         p_mode
            print     rmsg,rmsg_len
            jmp      next
p_mode:         
            print     pmsg,pmsg_len
next:
            SGDT  [GDTR]
            SIDT   [IDTR]
            SLDT  [LDTR]
            STR     [TR]
;           SMSW [MSW]
            print     gmsg, gmsg_len                                  ;GDTR (Global Descriptor Table Register)
                                                                                    ; LITTLE ENDIAN SO TAKE LAST WORD FIRST
            mov     ax,[GDTR+4]                                      ; load value of GDTR[4,5] in ax
            call       disp16_proc                                        ; display GDTR contents
            mov     ax,[GDTR+2]                                      ; load value of GDTR[2,3] in ax
            call       disp16_proc                                        ; display GDTR contents
            print     colon,1
            mov     ax,[GDTR+0]                                      ; load value of GDTR[0,1] in ax
            call       disp16_proc                                        ; display GDTR contents
            print     imsg, imsg_len                        ;IDTR (Interrupt Descriptor Table Register)
            mov     ax,[IDTR+4]              
            call       disp16_proc               
            mov     ax,[IDTR+2]              
            call       disp16_proc               
            print     colon,1
            mov     ax,[IDTR+0]              
            call       disp16_proc               
            print     lmsg, lmsg_len                        ;LDTR (Local Descriptor Table Register)
            mov     ax,[LDTR]                 
            call       disp16_proc               
            print     tmsg, tmsg_len                                    ;TR (Task Register)    
            mov     ax,[TR]                       
            call       disp16_proc               
            print     mmsg, mmsg_len                                ;MSW (Machine Status Word)          
            mov     ax,[MSW]                  
            call       disp16_proc               
            print     nline, nline_len
            exit
;--------------------------------------------------------------------         
disp16_proc:
            mov     rsi,char_sum+3                        ; load last byte address of char_sum buffer in rsi
            mov     rcx,4                                        ; number of digits
           
cnt:      mov     rdx,0                                        ; make rdx=0 (as in div instruction rdx:rax/rbx)
            mov     rbx,16                                      ; divisor=16 for hex
            div       rbx
            cmp     dl, 09h                                     ; check for remainder in RDX
            jbe       add30
            add      dl, 07h
add30:
            add      dl,30h                                      ; calculate ASCII code
            mov     [rsi],dl                                      ; store it in buffer
            dec      rsi                                            ; point to one byte back
            dec      rcx                                           ; decrement count
            jnz       cnt                                           ; if not zero repeat
           
            print char_sum,4                                 ; display result on screen
ret
;----------------------------------------------------------------
OUTPUT:
$ nasm -f elf64 protect.asm
$ ld -o pr5 protect.o
 ./pr5
Processor is in Protected Mode...
GDTR (Global Descriptor Table Register)     : 3DA09000:007F
IDTR (Interrupt Descriptor Table Register)   : FF578000:0FFF
LDTR (Local Descriptor Table Register)       : 0000
TR (Task Register)                                          : 0040
MSW (Machine Status Word)                        : 003B
$

0 comments:

Post a Comment