Vous êtes sur la page 1sur 7

CS-2202 Computer Organization and Assembly Language

Lab 6: Link Library Functions, Arithmetic Instructions

Objectives:
To understand basic link library functions.
To understand arithmetic instructions in assembly language.

Link Library Overview


A Link Library is a file containing procedures that have been compiled into machine
code. The code in a library begins as one or more source files containing procedures,
constants, and variables. The source files are assembled into object files, and the object
files are inserted into the library.

Calling a Library Procedure


Call a library procedure using the CALL instruction. Some procedures require input
arguments. The INCLUDE directive copies in the procedure prototypes (declarations).
The following example displays "1234" on the console:

INCLUDE Irvine16.inc
.code
mov ax,1234h ; input argument
call WriteHex ; show hex number
call Crlf ; end of line

Library Procedures - Overview


1) Clrscr - Clears the console and locates the cursor at the upper left corner.
Example:
call clrscr
2) Crlf - Writes an end of line sequence to standard output.
Example:
call crlf
3) Delay - Pauses the program execution for a specified n millisecond interval.
Example:
Mov ax,1000 ;1000 milisecond
call delay
4) DumpMem - Writes a block of memory to standard output in hexadecimal.
5) DumpRegs - Displays the AX, BX, CX, DX, SI, DI, BP, SP, EFLAGS, and
IP registers in hexadecimal. Also displays the Carry, Sign, Zero, and Overflow flags.
6) WriteBin - Writes an unsigned 32-bit integer to standard output in ASCII binary
format.
7) WriteChar - Writes a single character to standard output.
8) ReadChar - Reads a single character from standard input and the returns the
character in AL register. Example:
.data
char byte ?
.code

CASE Institute Page 1 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

call ReadChar
mov char , al
9) ReadInt - Reads an integer from standard input and the returns the integer in AX
register. Example:
.data
var byte ?
.code
call Readint
mov var, ax

10) WriteDec - Writes an unsigned 32-bit integer to standard output in decimal format.

11) WriteHex - Writes an unsigned 32-bit integer to standard output in hexadecimal


format.
12) WriteInt - Writes a signed 32-bit integer to standard output in decimal format.
13) WriteString - Writes a null-terminated string to standard output. When calling it,
place the string’s offset in EDX. For example
.data
Prompt BYTE “Enter Your Name: ”,0
.code
mov edx, OFFSET Prompt
call WriteString

Arithmetic Instructions:
The following table summarizes the arithmetic instructions used in the 8086
microprocessor. It also shows the effect of each instruction, a brief example and the
flags affected by the instruction. The “*” in the table means that the corresponding flag
may change as the result of executing the instruction. The “-“ means that the
corresponding flag is not affected by the instruction, whereas the “?” means that the
flag is undefined after the execution of the instruction.

CASE Institute Page 2 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

Addition:
ADD instruction adds a source operand to destination operand of same size. The syntax
is:
ADD destination, source
The source is unchanged by the operation and the sum is stored in the destination
operand.
The carry, zero, Sign, Auxiliary Carry, Parity flags are changed according to the values of
destination operand
Subtraction:
SUB instruction subtracts a source operand from destination operand of same size. The
syntax is:
SUB destination, source
The source is unchanged by the operation and the result is stored in the destination
operand. The CPU performs negation and then addition in order to complete the
subtraction operation.
The carry, zero, Sign, Auxiliary Carry, Parity flags are changed according to the values of
destination operand

Mutiply (MUL/IMUL Instruction)


There are two instructions for multiplying binary data. The MUL (Multiply) instruction
handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both
instructions affect the Carry and Overflow flag.

Syntax
The syntax for the MUL/IMUL instructions is as follows −

MUL/IMUL multiplier
Multiplicand in both cases will be in an accumulator, depending upon the size of the
multiplicand and the multiplier and the generated product is also stored in two
registers depending upon the size of the operands. Following section explains MUL
instructions with three different cases −

SN Scenarios

1 When two bytes are multiplied -

The multiplicand is in the AL register, and the multiplier is a byte in

CASE Institute Page 3 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

the memory or in another register. The product is in AX. High-order


8 bits of the product are stored in AH and the low-order 8 bits are
stored in AL.

2 When two one-word values are multiplied -

The multiplicand should be in the AX register, and the multiplier is a


word in memory or another register. For example, for an instruction
like MUL DX, you must store the multiplier in DX and the
multiplicand in AX.

The resultant product is a doubleword, which will need two


registers. The high-order (leftmost) portion gets stored in DX and
the lower-order (rightmost) portion gets stored in AX.

3 When two doubleword values are multiplied -

When two doubleword values are multiplied, the multiplicand


should be in EAX and the multiplier is a doubleword value stored in
memory or in another register. The product generated is stored in
the EDX:EAX registers, i.e., the high order 32 bits gets stored in the
EDX register and the low order 32-bits are stored in the EAX
register.

Division: (The DIV/IDIV Instructions)

CASE Institute Page 4 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

The division operation generates two elements - a quotient and aremainder. In case of
multiplication, overflow does not occur because double-length registers are used to
keep the product. However, in case of division, overflow may occur. The processor
generates an interrupt if overflow occurs.

The DIV (Divide) instruction is used for unsigned data and the IDIV (Integer Divide) is
used for signed data.

Syntax
The format for the DIV/IDIV instruction −

DIV/IDIV divisor
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or
32-bit operands. The operation affects all six status flags. Following section explains
three cases of division with different operand size −

SN Scenarios

1 When the divisor is 1 byte -

The dividend is assumed to be in the AX register (16 bits). After


division, the quotient goes to the AL register and the
remainder goes to the AH register.

2 When the divisor is 1 word -

The dividend is assumed to be 32 bits long and in the DX:AX


registers. The high-order 16 bits are in DX and the low-order 16
bits are in AX. After division, the 16-bit quotient goes to the AX

CASE Institute Page 5 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

register and the 16-bit remainder goes to the DX register.

3 When the divisor is doubleword -

The dividend is assumed to be 64 bits long and in the EDX:EAX


registers. The high-order 32 bits are in EDX and the low-order
32 bits are in EAX. After division, the 32-bit quotient goes to
the EAX register and the 32-bit remainder goes to the EDX
register.

CASE Institute Page 6 of 7


CS-2202 Computer Organization and Assembly Language
Lab 6: Link Library Functions, Arithmetic Instructions

Lab tasks

Lab Task No.1:


Write a program which reads two integers input by keyboard and display the sum and
product of these two digits. Call following procedures in your program for taking input
from keyboard and output to screen READINT, WRITINT. Also call dumpregs and crlf
(carriage return) procedures.

Lab Task No.2:


Write a program that calculates the remainder and quotient of two numbers entered by
user and display the answers.

NOTE: Get numbers from user.

Lab Task No.3:


Write a program which reads two integers from the user and tells the average of both
the numbers.

Lab Task No.4:


Check this mathematical trick for yourself.

Think of a number (you need to take user input for this number). Double it. Add six.
Divide your answer by two. Now take away the number you first thought of. The
number you get should be... three!

CASE Institute Page 7 of 7

Vous aimerez peut-être aussi