0% found this document useful (0 votes)
18 views9 pages

IA32 Primer

This document provides an introduction and overview of programming for IA 32 processors. It outlines three main objectives: understanding the IA 32 instruction set, using a systematic software development process, and programming in protected mode. Exercises are divided into three sections - assembly language programming, protected mode programming, and mixed language programming. Sample assembly language programs are provided, such as adding 32-bit numbers and switching from real to protected mode.

Uploaded by

dredd4becks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

IA32 Primer

This document provides an introduction and overview of programming for IA 32 processors. It outlines three main objectives: understanding the IA 32 instruction set, using a systematic software development process, and programming in protected mode. Exercises are divided into three sections - assembly language programming, protected mode programming, and mixed language programming. Sample assembly language programs are provided, such as adding 32-bit numbers and switching from real to protected mode.

Uploaded by

dredd4becks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Laboratory Primer: IA 32 Architecture

The main objective of this laboratory primer is to familiarize you with the following:

• Instruction set of IA 32 processors


• Program design with the help of a systematic software development procedure
• Programming in protected mode of IA 32 Architecture

To meet these objectives, a number of problems of varying complexities are provided in


this primer, grouped into three sections. If you are already familiar with the topics re-
lated to any of these sections, you can skip to the next section without loss of continuity.
The primer assumes that you have the following background:

• An understanding of any microprocessor


• The architecture of computers
• Programming in a high level language (preferably C)

The exercises in the primer are divided into the following 3 sections:

1. Assembly language programming for IA 32 Processors


2. Protected mode programming
3. Mixed language programming

ASSEMBLY LANGUAGE PROGRAMMING FOR IA 32 Processors

Problems given in this section are to be solved using the TASM assembler. Solutions to
these problems are to be implemented on Borland 3.1 version. For implementing your
programs, you are generally required to write programs using a simplified assembly lan-
guage, which goes with a line assembler. TASM is assumed to be used for solving prob-
lems given here. Use of other assemblers may require slight changes in some problem
specifications

The following template illustrates the general structure of a program

; Using the semicolon, comments can be inserted into a program


;
;
.model small ;defines one of the various segmentation models of the program
.stack 200h ; defines the stack portion of the program
.data ; defines the data segment portion

; Here the various variables can be defined and initialized


.code ; defines the code portion
; Includes the operations which are to be performed by the program

(Body of the program)

call proc ; CALL is followed by the name of the procedure.


proc endp ; indicates the end of the procedure
end ; end of the program

A standard program for 8086 (without comments) is shown below (Note that the assem-
bler is not case sensitive):
.MODEL small
.STACK 100H
.DATA

START_ADD DB 12H, 23H, 23H, 14H, 15H, 16H, 17H


SLENGTH DB 06H

CEDT, Jan 2005 1 of 9


Laboratory Primer: IA 32 Architecture

KEY_CHAR DB 23H
. CODE
start1: CLD
MOV AX, @DATA
MOV DS, AX
MOV AH, 00H
MOV AL, KEY_CHAR
MOV CL, 00H
MOV CH, SLENGTH
MOV BX, 0000H
CALL FIND
FIND PROC NEAR
BEGIN: MOV DL, START_ADD[BX]
CMP AL, DL
JE FOUND
INC BX
DEC CH
JNZ BEGIN
JMP STOP
FOUND: INC CL
INC BX
DEC CH
JNZ BEGIN
STOP: FIND ENDP
END start1
end
Sample programs for IA 32 processors

; 1. Addition of 32-bit numbers stored from location X to Y, result stored in location Z


dosseg
.model small
.386
.stack 100h
.data
X DD 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h
Y LABEL DWORD
Z EQU 0ffh
org 100h
.code
mov eax, @data
mov ds, ax ;initialise DS
mov eax, 0
lea ebx, X
lea ecx, Y
sub ecx, ebx
shr ecx, 2 ;Number of 32-bit values
mov edx, ecx
dec edx
next: add eax, [ebx+edx*4] ;repeated addition
dec edx
loop next
mov bx, Z ;store result
mov [bx], ax
mov ax, 4c00h ;exit
int 21h
end

CEDT, Jan 2005 2 of 9


Laboratory Primer: IA 32 Architecture

; 2. Program to add 32-bit numbers

.dosseg
.model small
.stack 100h
.586p

.data

numbers dd 44444444h, 11111111h , 22222222h , 22222222h


result dd ?

.code
start: mov eax, @data ; Initialize data segment.
mov ds,eax
mov esi, offset numbers ; Load Effective address of memory
; where numbers are stored.
mov ecx, 04h ; Load counter in ECX = total 32-bit numbers
mov ebx ,offset result ; Load Effective address of Result
mov edx,00h ; EDX =0 to hold and update carry.
mov dword ptr [ebx] , 00h ; set RESULT = 00h
drk: mov edi, dword ptr [esi]
add [ebx], edi ; Add the number.
jnc next ; Check carry.
inc edx ; If CY , increament carry reg=EDX
next: add esi , 04h ; Increament pointer
dec ecx ; Decrement counter
jnz drk ; If counter= 0 , then continue, else stop addition.
mov dword ptr [ebx + 04h], edx ; Store Addition in memory.
mov ax,4c00h ; Terminate execution.
int 21h
end start
end

; 3. Program to switch to protected mode


dosseg
.MODEL SMALL
.586p
codeseg_offset equ 08h
dataseg_offset equ 10h
extraseg_offset equ 18h
stackseg_offset equ 20h
;-----------------------------------------------------------------------------
; Segment Descriptor Structure
;-----------------------------------------------------------------------------
segment_descriptor struc
seg_length0_15 dw 0
base_addr0_15 dw 0
base_addr16_23 db 0
flags db 0
access db 0
base_addr24_31 db 0
segment_descriptor ends
;-----------------------------------------------------------------------------

CEDT, Jan 2005 3 of 9


Laboratory Primer: IA 32 Architecture

; Macro used for 32-bit address calculation


;-----------------------------------------------------------------------------
ADDR32 macro base,offsetadd
mov eax,0
mov ebx,eax
mov ax,base
shl eax,4
mov bx,offsetadd
add ebx,eax
endm
;-----------------------------------------------------------------------------
; Macro used for Displaying PE bit
;-----------------------------------------------------------------------------
DIS macro
mov ah,06H
mov bl,6
add al,30H
mov dl,al
int 21H
endm
dsetup macro dest
movzx eax, ax
shl eax, 4
mov dest.base_addr0_15, ax
shr eax, 8
mov dest.base_addr16_23, ah
endm
;-----------------------------------------------------------------------------
; Stack
;-----------------------------------------------------------------------------
stack16 segment para public use16
assume cs:code16, ds:data16
db 100 dup (?)
stack16_end label word
db 100 dup (?)
stack16 ends
;-----------------------------------------------------------------------------
; Data
;-----------------------------------------------------------------------------
data16 segment para public use16
assume cs:code16, ds:data16
MSG1 db "Switching to Protected Mode...Wait for sometime to switch back $"
MSG2 db "PE BIT OF CRO after switching back is $"
global_descriptor_table label fword
gdt_start dw gdt_size,0,0
dummy_des segment_descriptor <0,0,0,0,0,0>
code16_des segment_descriptor <0ffffh,0,0,9ah,0,0>
data16_des segment_descriptor <0ffffh,0,0,92h,0,0>
extraseg_des segment_descriptor <0ffffh,0,0,92h,0,0>
stak16_des segment_descriptor <00000h,0,0,96h,0,0>
gdt_size = $-(offset dummy_des)
data16 ends

CEDT, Jan 2005 4 of 9


Laboratory Primer: IA 32 Architecture

;-----------------------------------------------------------------------------
; Code starts
;-----------------------------------------------------------------------------
code16 segment para public use16
assume cs:code16, ds:data16
;-----------------------------------------------------------------------------
; Procedure used to implement delay
;-----------------------------------------------------------------------------
delay PROC NEAR
mov ecx,02ffffffH
lop: dec ecx
jnz lop
ret
delay endp
start16:
; turn off interrupts
cli
; Set up real mode registers
mov ax, data16
mov ds, ax
mov ax, stack16
mov ss, ax
mov sp, offset stack16_end
; Set up up the various pmode descriptiors
mov ax, code16
dsetup code16_des
mov ax, data16
dsetup data16_des
mov ax, stack16
dsetup stak16_des
; Set up the global descriptor table
ADDR32 data16,offset dummy_des
mov dword ptr ds:gdt_start[2],ebx
; Load Global Descriptor Table Register
lgdt fword ptr ds:global_descriptor_table
; Display a message before switching to Protected mode
mov dx,offset MSG1
mov ah,09H
int 21H
; Switch to Protected mode by setting PE bit of CR0
mov eax,cr0
or al,1
mov cr0,eax
db 0eah
dw $+4, codeseg_offset
; Set up the pmode descriptors
mov ax, dataseg_offset
mov ds, ax
mov ax, stackseg_offset
mov ss, ax
mov ax, extraseg_offset
mov es, ax
mov fs, ax
mov gs, ax

CEDT, Jan 2005 5 of 9


Laboratory Primer: IA 32 Architecture

; Give some delay in Protected mode


call delay
; Switch back to real mode by resetting PE bit of CR0
mov eax,cr0
and al,not 1
mov cr0,eax
db 0eah
dw $+4,code16
mov ax, data16
mov ds, ax
mov es, ax
mov ax, stack16
mov ss,ax
mov sp, offset stack16_end
; Turn interrupts back on and get back to DOS
sti
; Print the PE bit of CR0
mov dl,0AH
mov ah,02H
int 21H
mov dl,0DH
mov ah,02H
int 21H
mov dx,offset MSG2
mov ah,09H
int 21H
mov eax,cr0
and al,0001H
;Use the macro DISP to display PE bit of CR0
DISP
mov ax,4c00h
int 21h
code16 ends
end tart16

CEDT, Jan 2005 6 of 9


Laboratory Primer: IA 32 Architecture

Section 1: Assembly language programming of IA 32 Processors (These prob-


lems are associated with 32 bit registers)
1. Load registers eax, ebx, ecx and edx with the value AA55H. Write procedures for us-
ing various addressing modes.
2. Load registers DS and ES with the value 55AAH.
3. Write a procedure to read IA 32 Processor machine codes starting from a location X
to Y.
4. Fill a memory block from X to Y with a constant given in EAX register.
5. Move a block of memory whose starting address is given in DS: SI to a block whose
starting address is given in ES: DI and block length in CX. Your procedure should
work for fully overlapping, partially overlapping as well as non-overlapping blocks.
6. Find the largest number in a string of 32 bit numbers stored in memory locations
starting from X. The length of the string is given in location X-4. Put the result in lo-
cation X-8.
7. Search for a "key" character in a string of characters starting from location X. The
length of the string is given in location Y and the key in location Z.
8. Sort in ascending order a string of one byte numbers stored in memory. The address
of the first byte is given in location X and the length of the string in Y. Use bubble
sort and binary sort algorithms.
9. DS:SI gives starting address of a location to a string of characters. The length of the
string is given in ECX. Search for this string in a memory segment starting from loca-
tion whose address is given in ES: DI. If the search is successful clear the carry flag
else set it. Give the address of the first match in ES: DI. Make the procedure re-
entrant.
10. Memory locations starting from X contain machine codes of a procedure. Write a pro-
cedure to detect MOV instructions that may be present in the first 16 words of the
procedure.
11. Modify the above procedure to detect MOV instructions of 8086. Generalize your pro-
cedure to detect MOV instructions of any processor by giving input data as follows:
EAX contains a word that indicates the processor in question (0=8086, 1=8051 and
so on) and DS: SI contains the beginning of the procedure area in which the MOV in-
struction has been detected.
12. Write a procedure to generate a one-second delay. Use this procedure to write a
message on the display. (Do this by directly writing the message into the Video
Buffer of the display)
13. Write a procedure to implement an 8-bit software up/down counter. If the LSB of the
word in location X is 1, the counter must be disabled. The remaining 14 bits define
the clock rate applied to the counter with 0 signifying stoppage of counter. Display
the counter contents on the display device of the computer.
14. Write a procedure MULT to multiply two numbers in AX and BX and return the result
in CX and DX. Declare this procedure as public and write it such that it can be sepa-
rately assembled. Write another procedure called POLY which computes X**n
+X**(n-1)+.... +1 where n is given in BL and X in AX. Use the procedure MULT de-
fined above to compute this polynomial.
15. Data pertaining to students in a class is stored as a structure defined below in a data
segment

Offset in a data segment Contents


XX00 TO XXO7 NAME OF THE STUDENT
XXO8 HER or HIS CLASS (0=BE, 1=ME)
XX09 DEPARTMENT (0=CEDT…)
XX0A TO XX0C DATE OF BIRTH
XXOD NUMBER OF COURSES TAKEN
XX0E GRADE POINT AVERAGE
XXOF RESERVED

CEDT, Jan 2005 7 of 9


Laboratory Primer: IA 32 Architecture

Write a procedure to read any data of any student from this table. Students are iden-
tified by serial numbers and the serial number of the student whose data is required
is given in AL. The item of data requested is given in AH. Clear the carry if the re-
quested data is obtained and set the carry if invalid request for data is noticed. Dis-
play the output of your procedure on the display.
16. Do the operations given in the following table on a data block starting from a location
pointed to by DS: SI and of length given in CX.

Contents of location X Operation


1 Fill with data
2 Complement data
3 Multiply data by 2
4 Divide data by 2
5 Fill with data in AX
Any other value Set carry to 1, display error

17. Write four procedures A (add) S (sub), M (multiply), and D (divide) of two 16-bit
numbers. Write another procedure CNT which executes A if AX=0, S if AX=1, M if
AX=2, D if AX=3. For any other AX it returns an error code without executing any of
the above procedures.
18. Write a recursive procedure to generate words composed of Roman alphabets. Words
are generated by repeatedly calling a random generator and assigning 1=a, 2=b,
...26=z. A word is considered to be fully formed when the random number generated
is 0. Place the generated word in location starting from X terminating it with a 0. Re-
turn the pointer to this word in DS: SI.
19. Write an assembly program to print the message "This machine is operating in real
mode" on the screen. Operate IA 32 in real mode with extended registers.
20. Add a series of 32 bit numbers stored in locations X to Y and store the result in loca-
tion Z.
21. Use REP STOSD to clear the screen.
22. Write a program to display the current status of all the IA 32 flags. This involves
reading the flag register and individually separating out the flags for displaying.
23. A string of 32 bit numbers is available starting from a location X. The length of the
string is specified in a location Y. Each of the numbers of the string is read, comple-
mented and stored starting form location Z. Write programs to do the above task
with 32 bit operands and 16 bit operands. Compare the execution times for both
these programs.
24. Two multi-byte binary numbers are stored in memory starting form locations X to Y.
Add /subtract these two numbers and store the result in memory locations starting
from Z.
25. Modify the procedure if the bytes are packed and unpacked BCD numbers.
26. Two multi-byte binary numbers are stored in memory starting from locations X and
Y. Multiply these two numbers and store the result in memory locations starting from
Z.
27. Implement a four-function calculator to operate on two digit decimal numbers given
in AX and BX.
28. Write a recursive procedure to calculate the factorial of a one-byte number stored at
location X. Store the result at locations starting from Y.
29. Generate all 16-bit Fibonanci numbers.
30. Find all 16 bit prime numbers.
31. Find the square root of a positive number in the range 1 to FFFFH using the Newton-
Raphson method.
32. Write a procedure to convert a four digit hexadecimal number to its ASCII equivalent
using the XLAT instruction.
33. Convert a binary number into a BCD number and a BCD number into a binary num-
ber.

CEDT, Jan 2005 8 of 9


Laboratory Primer: IA 32 Architecture

34. Write a procedure to find the checksum of 16 bit numbers located in memory from X
to Y.
35. A code word is stored in a location X. Write a procedure to check if it is a valid 7,4
hamming code. Set the location Y to 0 if the codeword is a hamming code and to FF
otherwise.
Section 2: This section deals with programming in protected mode

(Note: To run protected mode programs below, you must write your programs to run
under DOS and not WINDOWS. Start the PC in the DOS mode)
1. Use the struc directive to create the descriptor table structures. Define various tem-
plates to define a descriptor as global, local, present, task, descriptor, privilege level
00 etc. Using the structure defined above, create a GDT and an IDT at suitable loca-
tions for accommodating 10 global descriptors and 40 interrupts. Fill all the neces-
sary data like base address, limit etc.
2. Extend the above routine to prepare IA 32 to switch to protected mode.
3. Write a program in real mode to write some arbitrary data on the screen directly
without using DOS calls.
4. After preparing IA 32 processor to switch to protected mode, switch to protected
mode. Write some arbitrary data on the video screen. For this the video memory
must be considered as a data segment and data must be written directly into the
memory. This data segment must be defined through suitable segment descriptors.
In this simple exercise don't use any local descriptors, interrupts and task segments.
After writing data into the buffer, the program should return the machine to the real
mode.
Section 3: This section deals with mixed language programming
1. Write a procedure MULT to multiply two numbers in AX and BX and return the result
in CX and DX. Declare this procedure as public and write it such that it can be sepa-
rately assembled. Write another procedure called POLY in C language which com-
putes X**n+X**(n-1) +...+1 where n is given in BL and X in AX. Use the procedure
MULT defined above to compute this polynomial.
2. Write a procedure in C to receive an ASCII character from the keyboard. Call an as-
sembly procedure called CONV from the C program which converts lower case letters
to capitals, does not alter the character if it is not an alphabet and returns it to its
calling procedure. Display the character returned.
3. Write a procedure in C to receive a character from the keyboard. Call an assembly
procedure "DELAY" to produce a delay of X milliseconds. Display successful return to
C procedure with a suitable message.
4. Write a procedure in C to receive a character from the keyboard. Call an assembly
procedure to load this character into a block of memory starting from location X and
whose length is defined in the C procedure.
5. Write a procedure in C to receive two-digit decimal numbers as well as a string ter-
minated by the "carriage return" from the keyboard. Call assembly procedures A, S,
M and D to add, subtract, multiply and divide these numbers if the string received is
ADD, SUB, MUL and DIV respectively. Return error conditions for any other input
string as well as numbers greater than two decimal digits. Write also a C function to
display the result.
6. Write an assembly procedure to truncate a string to its first eight characters. Call a C
procedure to obtain the string up to a length of 128 characters and pass it to the as-
sembly procedure for processing.

Use of tools:

Use tasm /Zi filename for assembling source file; Command: tasm /Zi filename
Use tlink/v to link the file; Command: Tlink/v filename

CEDT, Jan 2005 9 of 9

You might also like