section .data
	; Define strings and their lengths
	introMsg dw "ALP to detect the operating mode of the microprocessor and display the contents of some system registers"
	introMsgLen equ $ - introMsg
    
	gdtrMsg db 10,10,"Contents of GDTR : ",10
	gdtrMsgLen equ $ - gdtrMsg
    
	ldtrMsg db 10,10,"Contents of LDTR : ",10
	ldtrMsgLen equ $ - ldtrMsg
    
	idtrMsg db 10,10,"Contents of IDTR : ",10
	idtrMsgLen equ $ - idtrMsg
    
	trMsg db 10,10,"Contents of TR : ",10
	trMsgLen equ $ - trMsg
    
	mswMsg db 10,10,"Contents of MSW (CR0) register : ",10
	mswMsgLen equ $ - mswMsg
    
	protectedMsg db 10,10,"The Microprocessor is in the protected mode"
	protectedMsgLen equ $ - protectedMsg
    
	proMsg db 10,10,"The contents of the system registers are as follows : "
	proMsgLen equ $ - proMsg
    
	realMsg db 10,10,"The Microprocessor is in the real mode"
	realMsgLen equ $ - realMsg
    
	colon db "  :  "
	colonLen equ $ - colon
    
section .bss
	; Define uninitialized memory to store system register values
	gdtr resd 1 ;to store 48-bit GDTR value-resd(32-bit) and resw(16-bit).
     	resw 1
    	 
	ldtr resw 1 ;to store 16-bit LDTR
    
	idtr resd 1  ;to store 48-bit IDTR value-resd(32-bit) and resw(16-bit).

     	resw 1
    	 
	tr resw 1
    
	msw resd 1
    
	result resb 4
    
%macro write 2
	; Define a macro for writing to stdout using syscall
	mov rax,1        	; syscall number for sys_write
	mov rdi,1        	; file descriptor 1 (stdout)
	mov rsi,%1       	; pointer to the message to be printed
	mov rdx,%2       	; length of the message
	syscall
%endmacro

section .text
	global _start

_start:
     	; Print introductory message
     	write introMsg,introMsgLen
   	 
     	smsw eax         	; Store the contents of the MSW register into the EAX register.
     	bt eax,0         	; Copy the 0th least significant bit from EAX to the carry flag.
    	 
     	jc protected_mode	; If CF is set, processor is in protected mode
    	 
     	write realMsg,realMsgLen  ; Print message indicating real mode
    	 
     	jmp endOfProgram	; Jump to end of program
    	 
protected_mode :
     	write protectedMsg , protectedMsgLen  ; Print message indicating protected mode
    	 
     	write proMsg , proMsgLen           	; Print message indicating contents of system registers
    	 
     	sgdt [gdtr]     	; Store the content of the global descriptor table register (GDTR) in gdtr
     	sldt [ldtr]     	; Store LDTR
     	str [tr]        	; Store TR
     	smsw [msw]      	; Store MSW
    	 
     	; Print contents of GDTR
     	write gdtrMsg,gdtrMsgLen
     	mov bx,[gdtr+4]  ; Move upper half of GDTR into bx reg.
     	call disp
     	mov bx,[gdtr+2]
     	call disp
     	write colon , colonLen
     	mov bx,[gdtr]
     	call disp
    	 
     	; Print contents of LDTR
     	write ldtrMsg,ldtrMsgLen
     	mov bx,[ldtr]
     	call disp
    	 
     	; Print contents of IDTR
     	write idtrMsg,idtrMsgLen
     	mov bx,[idtr+4]
     	call disp
     	mov bx,[idtr+2]
     	call disp
     	write colon , colonLen
     	mov bx,[idtr]
     	call disp
    	 
     	; Print contents of TR
     	write trMsg,trMsgLen
     	mov bx,[tr]
     	call disp
    	 
     	; Print contents of MSW
     	write mswMsg,mswMsgLen
     	mov bx,[msw+2]   ; Upper half of CR0 or MSW
     	call disp
     	mov bx,[msw]
     	call disp
    	 
endOfProgram:    
	; Terminate the program
	mov rax, 60
	mov rdi, 0
	syscall
    
disp:
    	; Procedure to convert binary to ASCII and print
    	mov rdi, result  	; Point rdi to result variable
    	mov cx,04        	; Load count of rotation in cl
up1:
    	rol bl,04        	; Rotate number left by four bits
    	mov dl,bl        	; Move lower byte in dl
    	and dl,0fh       	; Get only LSB
    	cmp dl,09h       	; Compare with 39h
    	jg add_37        	; If greater than 39h skip add 37
    	add dl,30h       	; Else add 30
    	jmp skip1        	; Skip to add_37
add_37:
    	add dl,37h       	; Add 37
skip1:  
    	mov [rdi],dl     	; Store ASCII code in result variable
    	inc rdi          	; Point to next byte
    	dec cx           	; Decrement the count of digits to display
    	jnz up1          	; If not zero, repeat
   	 
    	write result , 4 	; Write the result to stdout
   	 
    	ret               	; Return

 
by

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret