IA32 Primer
IA32 Primer
The main objective of this laboratory primer is to familiarize you with the following:
The exercises in the primer are divided into the following 3 sections:
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
A standard program for 8086 (without comments) is shown below (Note that the assem-
bler is not case sensitive):
.MODEL small
.STACK 100H
.DATA
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
.dosseg
.model small
.stack 100h
.586p
.data
.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
;-----------------------------------------------------------------------------
; 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
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.
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.
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