SE Computer Engineering

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

;This program first check the mode of processor(Real or Protected),
;then reads GDTR, LDTR, IDTR, TR & MSW and displays the same.

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

section .data
rmodemsg db 10,13,”Processor is in Real Mode”
rmsg_len equ $-rmodemsg

pmodemsg db 10,13,”Processor is in Protected Mode”
pmsg_len:equ $-pmodemsg

gdtmsg db 10,13,”GDT Contents are::”
gmsg_len:equ $-gdtmsg

ldtmsg db 10,13,”LDT Contents are::”
lmsg_len equ $-ldtmsg

idtmsg db 10,13,”IDT Contents are::”
imsg_len equ $-idtmsg

trmsg db 10,13,”Task Register Contents are::”
tmsg_len equ $-trmsg

mswmsg db 10,13,”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

section .text
global _start

smsw eax        ;Reading CR0
mov [cr0_data],eax
bt eax,0        ;Checking PE bit(LSB), if 1=Protected Mode, else Real Mode
jc prmode
disp rmodemsg,rmsg_len
jmp nxt1

disp pmodemsg,pmsg_len

sgdt [gdt]
sldt [ldt]
sidt [idt]
str [tr]

disp gdtmsg,gmsg_len
mov bx,[gdt+4]
call disp_num

mov bx,[gdt+2]
call disp_num

disp colmsg,1
mov bx,[gdt]
call disp_num

disp ldtmsg,lmsg_len
mov bx,[ldt]
call disp_num

disp idtmsg,imsg_len
mov bx,[idt+4]
call disp_num

mov bx,[idt+2]
call disp_num

disp colmsg,1
mov bx,[idt]
call disp_num

disp trmsg,tmsg_len
mov bx,[tr]
call disp_num

disp mswmsg,mmsg_len
mov bx,[cr0_data+2]
call disp_num

mov bx,[cr0_data]
call disp_num

disp nwline,1

mov eax,01
mov ebx,00
int 80h

mov esi,dnum_buff     ;point esi to buffer
mov ecx,04        ;load number of digits to display

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

mov [esi],dl        ;store ASCII code in buffer
inc esi            ;point to next byte
dec ecx
jnz up1            ;decrement the count of digits to display
            ;if not zero jump to repeat
disp dnum_buff,4    ;display the number from buffer

swlab@swlab-H81-M1:~$ nasm -f elf msw.asm
swlab@swlab-H81-M1:~$ ld -m elf_i386 -s -o msw msw.o
swlab@swlab-H81-M1:~$ ./msw

Processor is in Protected Mode
GDT Contents are::1FA89000:007F
LDT Contents are::0000
IDT Contents are::FF578000:0FFF
Task Register Contents are::0040
Machine Status Word::80050033

Leave a Reply

Your email address will not be published.