Sie sind auf Seite 1von 50

2011

UST Faculty of Engineering ICS Department

LECTURE NOTES IN COMPUTER ORGANIZATION


A Compilation of Notes and Exercises in Computer Organization & Assembly Language Programming for CS, IS and IT students of the ICS Department

Review of the Number Systems


Decimal Base 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hexadecimal Base 16 0 1 2 3 4 5 6 7 8 9 A B C D E F Binary Base 2 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal numbers are numbers used by computers instead of binary to assist in reading memory contents, among other things. It is a base 16 number system, which means 16 digits represent all numbers instead of just two, like binary. Addition/Subtraction of Hexadecimal Numbers It's just like decimal, however, when you "carry the...", the number you carry is different. In hexadecimal, it's 16; in octal, it's 8 and in binary, it's 2. Do the arithmetic in the given base. The principle is exactly the same as it is in decimal for subtraction or addition - carry the ones, etc. Just keep in mind what base you're in and don't accidentally apply decimal concepts (e.g. one more than 7 is 10 in octal). Let's do a pair of decimal problems and see if we can "parallel" the solution method in hex. Suppose we want to subtract 1586 - 243 (in decimal). We write the subtrahend (the number to be subtracted) underneath the minuend (the number to subtract from), lining up the place values at the decimal point - or at the right if there is no decimal point - like this:
1586 243 ----

Now, starting from the right, we subtract the digits. (Ones place) 6-3 = 3, (tens place) 8-4 = 4, (hundreds place) 5-2 = 3, (thousands place) 1-0 = 1. It looks like this:
1586 243 ---1343

UST Faculty of Engineering

J.A.Agbayani

Page 2

We do the same thing in hexadecimal, except that we occasionally have a digit larger than 9. For example, suppose we want to subtract 5CD2 - 2A0 (hexadecimal). As with decimal, we write the subtrahend (the number to be subtracted) underneath the minuend (the number to subtract from), lining up the place values at the radix point - or at the right if there is no radix point - like this:
5CD2 h 2A0 h -----(The 'h' just indicates a hexadecimal value)

Now, starting from the right, we subtract the digits. (Ones place) 2-0 = 2, (sixteens place) D-A = 13-10 = 3, (256's place) C-2 = 12-2 = 10 = A, (4096's place) 5-0 = 5. It looks like this:
5CD2 h 2A0 h -----5A32 h

That's fine - until you get a place value in which there's not enough to subtract from. What do we do in decimal? We borrow from the next higher place value. For example, suppose we want to subtract 3145 - 1976 (decimal). We'll align our numbers:
3 1 4 5 - 1 9 7 6 ----------

Now in the ones place, we can't subtract 6 from 5 (5 is less than 6) so we borrow 1 from the tens place. Since that has a place value (a "weight") of ten, we can exchange it for 10 ones. (This is kind of like changing a P10 bill into ten P1 bills.) This leaves us with 3 tens (4 minus the 1 we borrowed) and gives us 15 ones (5 plus the 10 we got from the borrow). We now subtract 15-6 = 9:
3 15 3 1 \ \ - 1 9 7 6 ---------9 (There was a 4 and a 5 under the '\'s)

Similarly in the tens place, we can't subtract 7 from 3 so we borrow one from the hundreds place. Since that has a place value (a "weight") of one hundred, we can exchange it for 10 tens. (This is kind of like changing a P100 bill into ten P10 bills.) This leaves us with no hundreds (1 minus the 1 we borrowed) and gives us 13 ones (the 3 left over after the previous borrow plus the 10 we got from this borrow). We now subtract 13-7 = 6:
0 13 15 3 \ \ \ - 1 9 7 6 ---------6 9

UST Faculty of Engineering

J.A.Agbayani

Page 3

We do the same for the hundreds, borrowing 1 from the thousands, then subtract the thousands. We end up like this:
2 10 13 15 \ \ \ \ - 1 9 7 6 ---------1 1 6 9

Why did we get 10 from each borrow? Because each time, the next place value was 10 times as large. This is because decimal is base-10. Now let's look at a hexadecimal problem that requires borrowing. For example, suppose we want to subtract A8D2 - 3EAC (hexadecimal). We'll align our numbers:
A 8 D 2 h - 3 E A C h -------------

Now in the ones place, we can't subtract C (12) from 2 so we borrow 1 from the sixteens place. Since that has a place value (a "weight") of sixteen, we can exchange it for 16 ones. (This is kind of like changing a P16 bill into sixteen P1 bills.) This leaves us with 12 sixteens (D = 13 minus the 1 we borrowed) and gives us 18 ones (2 plus the 16 we got from the borrow). We now subtract 18-12 = 6:
12 18 A 8 \ \ h - 3 E A C h ------------6 h (Note that I use decimal here. Some people write these as Ch and 12h.) (There was a D and a 2 under the '\'s)

In the sixteens place, we don't need to borrow because we can subtract 10 (A) from 12:
12 18 A 8 \ \ h - 3 E A C h ------------2 6 h

In the 256's place, we again need to borrow. We'll borrow 1 from the 4096's place and exchange it for sixteen 256's (one 4096 equals sixteen 256's). This leaves us 9 in the 4096's place (A = 10 minus the 1 that we borrowed), and gives us 24 in the 256's place (8 plus the 16 from the borrow). We then can subtract 24-14 = 10 = A. So we have:
9 24 12 18 \ \ \ \ h - 3 E A C h ------------A 2 6 h

UST Faculty of Engineering

J.A.Agbayani

Page 4

Finally, we subtract 9-3 = 6 in the 4096's place:


9 24 12 18 \ \ \ \ h - 3 E A C h ------------6 A 2 6 h

Why did we get 16 from each borrow? Because each time, the next place value was 16 times as large. This is because hexadecimal is base-16. One final note: If the subtrahend (the bottom number) is larger than the minuend (the top number), flip the numbers around and make the final answer negative, just as you would in decimal.

Multiplication of Hexadecimal Number


All the operations work exactly the same in any base, as long as you use the right tables and remember to use the base when you carry or borrow. For example, here's 25 x 3A (37 x 58 base 10):
25 x 3A ---32 14 F 6 ---862 =

A A 3 3

x x x x

5 2 5 2

(10 x 5 = 50) (10 x 2 = 20) (3 x 5 = 15) (3 x 2 = 6)

2146 (base 10)

I wrote out each product of a pair of digits here, to save carrying, and to show explicitly the four numbers you would get from a multiplication table (though I just multiplied each pair in base 10 and converted to base 16); normally you'd write it as
25 x 3A ---172 6F ---862

= 32 + 140 = F + 60

Here, for example, 5 x A = 32, so I put down 2 and carried the 3, which I added to the result of A x 2 = 14 to get 17. When I added the partial products, I found that 7+F = 16, so I put down the 6 and carried the 1.

UST Faculty of Engineering

J.A.Agbayani

Page 5

IT 103 Exercise 1 UST Faculty of Engineering

Name: ______________________ Section: ______ Date: _____________

1. Perform the following hexadecimal additions and subtraction: a) 23ABH + 0AC34H b) 9458H + 9977H c) 0F0BH + 8FFFH d) 296BH + 7A4FH e) 6901H + 996FH f) 1111H + 0FFFFH g) 734BH 9F3BH h) 9938H 34FFH i) 290DH 1FA8H j) 0F343H E229H k) 5FEFH 1212H l) 89CBH 123CH _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________

2. Perform the following binary additions and subtractions: a) 110111 + 1111 b) 111101 + 1011 c) 100001 1111 d) 101000 1011 _________________________ _________________________ _________________________ _________________________

3. Perform the following hexadecimal multiplications: a) 80A * 21 b) 521 * 1B c) 46F1 * A d) 90 * C2 _________________________ _________________________ _________________________ _________________________

UST Faculty of Engineering

J.A.Agbayani

Page 6

Organization of the IBM PC

UST Faculty of Engineering

J.A.Agbayani

Page 7

A Summary of 8086-family Arithmetic Instructions. ADD op1,op2 op1 = op1 + op2. Allowed formats: op1 op1 Register number Memory number Register register Memory register Register memory op1 = op1 op2. as in ADD above. op = op + 1 op = op 1 op = 0 op if op is a byte, then AH, AL = op * AL If op is a word, then DX, AX = op * AX if op is a byte, then AL/op = AL remainder AH if op is a word, then AX/op = AX remainder DX Allowed formats:

SUB op1,op2 INC op DEC op NEG op MUL op DIV op

What will be the contents of the AX, BX and CX registers after executing each of the following program fragments? AX MOV ADD MOV MOV ADD SUB MUL AX,0 AX,6 BX,4 CX,5 AX,BX AX,3 BX BX CX DX

What will be the contents of the AH, AL, BH, BL, CH, CL, DH, and DL registers after executing each of the following program fragments? AH MOV MOV MOV SUB ADD ADD MOV DL,3 CX,5 BH,DL CX,7 AH,2 CH,5 DX,AX AL BH BL CH CL DH DL

UST Faculty of Engineering

J.A.Agbayani

Page 8

IT103 Exercise 2 UST Faculty of Engineering

Name: __________________________ Section: _____ Date: _________________

What will be the contents of the registers after executing each of the following program fragments? Program 1 MOV DX,8 MOV AX,9 SUB DX,4 MUL DX MOV CX,DX INC CX SUB CX,1 MOV BX,CX Program 2 MOV AX,2 MUL AX MUL AX MUL AX MOV BX,3 ADD AX,BX ADD AX,BX MUL BX Program 3 MOV AX,0H MOV BX,34H MOV CX,35H MOV DX,0FH ADD BX,DX SUB CX,01CH ADD AX,BX INC CX MUL CX Program4 MOV AX,2H MOV BL,34H MOV CH,35H MOV DX,0FH MOV CL,DL MOV BH,CH ADD BX,DX SUB CX,01CH ADD BL,CH INC CX MUL CX AX BX CX DX

AX

BX

CX

DX

AX

BX

CX

DX

AH

AL

BH

BL

CH

CL

DH

DL

UST Faculty of Engineering

J.A.Agbayani

Page 9

Multiplication & Division


MUL MUL MUL MUL MUL MUL MUL MUL MUL MUL MUL MUL DIV DIV DIV DIV DIV DIV DIV DIV DIV DIV DIV DIV AX BX CX DX AH AL BH BL CH CL DH DL AX BX CX DX AH AL BH BL CH CL DH DL DX,AX DX,AX DX,AX DX,AX AH,AL AH,AL AH,AL AH,AL AH,AL AH,AL AH,AL AH,AL AX AX AX AX AL AL AL AL AL AL AL AL = = = = = = = = = = = = = = = = = = = = = = = = AX BX CX DX AH AL BH BL CH CL DH DL * * * * * * * * * * * * AX AX AX AX AL AL AL AL AL AL AL AL to to to to to to to to to to to to DX DX DX DX AH AH AH AH AH AH AH AH

AX/AX AX/BX AX/CX AX/DX AL/AH AL/AL AL/BH AL/BL AL/CH AL/CL AL/DH AL/DL

remainder remainder remainder remainder remainder remainder remainder remainder remainder remainder remainder remainder

Program 1 MOV BX,4 MOV AX,8 MUL BX MOV DX,5 MUL DX Program 2 MOV BH,A8 MOV AL,6 MUL BH Program 3 MOV BX,4 MOV AX,7 DIV BX Program 4 MOV BL,4 MOV AL,A8 DIV BL
UST Faculty of Engineering

AX

BX

DX

AH

AL

BH

AX

BX

DX

AH

AL

BL

J.A.Agbayani

Page 10

Write fragments of Intel 8086-family assembly language which will: 1. Leave in AX the value of a+b-c where a, b, c denote the signed 16-bit contents of AX, BX and CX respectively. MOV MOV MOV ADD SUB AX,a BX,b CX,c AX,BX AX,CX

2. Leave in AX the value of a*b-c where a,b,c denote the signed 16-bit contents of AX, BX and CX. MOV MOV MOV MUL SUB AX,a BX,b CX,c BX AX,CX

3. Leave in AX the value of a+b/3 where a, b,3 denote the signed 16-bit contents of AX, BX and CX. MOV MOV MOV DIV ADD CX,a AX,b BX,3 BX AX,CX

UST Faculty of Engineering

J.A.Agbayani

Page 11

IT103 Exercise 3 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

Write fragments of Intel 8086-family assembly language which will: 1. Leave in AX the value of (a-b)/(c-d) where a, b, c, d denote the signed 16-bit contents of AX, BX, CX and DX and where we assume that c d is not zero.

2. Leave the average value (a+b+c)/3 in register AX where a, b, c,3 are as in (1) above.

3. Leave the value of (b*c)/a in register AX where a, b, c are as in (2) above and it is assumed that the content of AX is not zero.

UST Faculty of Engineering

J.A.Agbayani

Page 12

SEGMENTED MEMORY In order to understand the important concept of segmented memory, which is used by all members of the INTEL 8086 family, let us fix our attention on just one microprocessor for a moment, namely the 8086 microprocessor. It allows memory addresses to consist of 20 bits. Thus, when executing an instruction which involves fetching a data item from memory, the 20-bit address of that item will be sent along the computers address bus to tell the memory circuits where the required data item is to be found. Hence the address bus has 20 lines, one for the transmission of each bit of the address. The use of a 20-bit address means that the 8086 microprocessor has the ability to access: 220 = 1,048,576 different memory locations, numbered from: 0000 0000 0000 0000 0000 (0 in decimal) to: 1111 1111 1111 1111 1111 (1,048,576 in decimal) We must recall at this point that the instructions in a machine code program are themselves stored in memory. The 8086 microprocessor instructions were designed to take up a maximum of six bytes given that two bytes were allowed for the name of a particular instruction. It follows that if a 20-bit address such as 20BAFH were allowed, then an instruction of the form check if the value stored at address 20BAFH is 365 would require: 2 bytes for the instruction name; 2 bytes to represent 365 as an unsigned 16-bit number; and 3 bytes for the address 20BAFH thus exceeding the designers self-imposed 6-byte instruction length limit. To avoid this (and because there are other advantages, as shall be pointed out later), it was decided that the 8086 family would employ the segmented memory concept. Rather than representing each address as a 20-bit unsigned number, memory is thought of as being divided up into segments each of which contains 216 locations. In this way an address can be thought of as consisting of two parts: a 16-bit segment address and a 16-bit offset from the start of that segment. SPECIFYING ADDRESSES To represent a segment address and its relative offset we use the notation: <segment address>:<offset address> Thus, 020A:1BCD denotes offset 1BCDH from segment 020AH. The actual address it refers to is obtained in the following way: 1. Add a zero to the right-hand side of the segment address. 2. Add to this the offset. Hence the actual address referred to by 020A:1BCD is 03C6DH. 020A0H 1BCDH ?

UST Faculty of Engineering

J.A.Agbayani

Page 13

IT103 Exercise 4 UST Faculty of Engineering

Name: __________________________ Section: _____ Date: _________________

1. Write the following segment:offset addresses as absolute (actual) address. a) b) c) d) e) 26AB:1234 FEDC:AAAA A5B9:9999 1234:26AB 1991:1992 __________________________ __________________________ __________________________ __________________________ __________________________

2. Write the following absolute addresses in segment:offset form where the segment address is 4AB1H. a) b) c) d) 4AB10H 51000H 4FFFFH 55555H ____________:_____________ ____________:_____________ ____________:_____________ ____________:_____________

3. For each of the following pairs of addresses given in segment:offset form, decide if the two addresses correspond to the same memory location. Write Equal/Not Equal on the space provided. a) 74D6:0100 b) 4E50:F10F c) 1234:1234 d) 0500:ABCD 74C6:0110 4C50:0FF0 1358:0040 0EB0:10CD ____________ ____________ ____________ ____________

4. For each of the following pairs of addresses given in segment:offset form, encircle the pair that represents higher absolute address. a) BCEF:0123 b) DE12:AB1C c) 0AEB:C1D2 BCFE:2412 DD12:BB1D 0DFB:024B

UST Faculty of Engineering

J.A.Agbayani

Page 14

JUMPS AND LOOPS

JMP - Jump (no matter what) NAME


JZ JNZ JA JB JAE JBE JG JL JGE JLE JE JNE

ALIAS Testing for zero


Zero Not zero

JUMP IF

Comparing unsigned numbers


JNBE JNAE JNB JNA Above Below Above or equal Below or equal Greater Less Greater or equal Less or equal

Comparing signed numbers


JNLE JNGE JNL JNG

The three special instructions for the 8086 family which are designed to help in the efficient implementation of loops are LOOP <label> MOV CX,3D MOV AX,1D INC AX LOOP in jump to <label> while CX is non-zero. AX CX

in:

LOOPZ <label> MOV CX,3D MOV AX,3D MOV BX,1D DEC AX INC BX SUB AX,BX LOOPZ in

loop while the outcome is zero (equal) and CX is non-zero. AX BX CX

In:

LOOPNZ <label> MOV CX,2D MOV AX,8D MOV BX,1D DEC AX INC BX SUB AX,BX LOOPNZ in

loop while the outcome is non-zero and CX is non-zero AX CX

in:

UST Faculty of Engineering

J.A.Agbayani

Page 15

Translate the following Java/C++ program fragments into Assembly format: a) if (AX <= 5) BX = 4; else BX = 6;

b) for(AX=0, CX=1;CX<=5;CX++) AX=AX+1;

UST Faculty of Engineering

J.A.Agbayani

Page 16

IT103 Exercise 5 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

1. Translate the following Java/C++ program fragments into Assembly format: a) if (AX <= 5 && BX >= 6) BX = 4; else BX = 6;

b) AX=0; do{ AX=AX+1; }while (AX<=5);

2. How could an ifelse statement involving an AND be implemented in assembly language. Illustrate with reference to: if (AX<3 && BX<2) CX=1; else CX=2;

3. How could an if .. then .. else statement involving an or be implemented in assembly language. Illustrate with reference to: if (AX < 3 || BX < 2) CX = 3; else CX = 4;

UST Faculty of Engineering

J.A.Agbayani

Page 17

Assembly Input/Output Commands Input a character Destination register is AL MOV AH,1 INT 21H Output a character Source register is DL MOV DL,a MOV AH,2 INT 21H Output a string Source register is DX .DATA Myname DB Jess$ .CODE LEA DX,myname ;MOV DX,OFFSET myname MOV AH,9 INT 21H Problem 1. Assuming that the user types character d, what is the output of the program fragment below? AH AL DL Screen MOV AH,1 INT 21H MOV DL,AL MOV AH,2 INT 21H INC DL INT 21H INC DL INT 21H INT 21H Problem 2. Using the LOOP statement, write an assembly program fragment that displays the alphabet from A to E.
? ? ? ? ? ? ?

UST Faculty of Engineering

J.A.Agbayani

Page 18

;Filename: sample.asm - Input/Output a Character .MODEL small .STACK 64 .DATA prompt1 DB "Input a character: $" prompt2 DB 0AH,0DH,"You typed: $" prompt3 DB 0AH,0DH,"Want More? $" .CODE main PROC FAR MOV AX,@data MOV DS,AX start: MOV AX,3 INT 10H MOV DX,OFFSET prompt1 MOV AH,9 INT 21H MOV AH,1 INT 21H MOV BL,AL MOV DX,OFFSET prompt2 MOV AH,9 INT 21H MOV DL,BL MOV AH,2 INT 21H MOV DX,OFFSET prompt3 MOV AH,9 INT 21H MOV AH,1 INT 21H CMP AL,'Y' JE start CMP AL,'y' JE start MOV AH,4CH INT 21H ENDP END main ;clrscr();

;cout<<prompt1; ;press a character ;store character to BL ;cout<<prompt2

;display character ;Do you want more?

;press any key

;jump if equal to start label

;exit to DOS ;end of program

main

Program OUTPUT: Input a character: A <Enter> You typed A Want more? Y

UST Faculty of Engineering

J.A.Agbayani

Page 19

Required Programs
TASM.exe TLINK.exe

To compile and run the program STEP 1. Open the MS-DOS command prompt. STEP 2. Compile the program (sample.asm)

STEP 2. Link the program (sample.obj)

STEP 3. Execute the program (sample.exe)

UST Faculty of Engineering

J.A.Agbayani

Page 20

STORING AND RETRIEVING DATA FROM MEMORY [ ] The content of memory location pointed to by an address. Data can be transferred from memory into a register one or two bytes at a time. The instruction MOV MOV MOV MOV MOV AL,[200H] BX,[200H] AX,200H [200H],CL [200H],DX will transfer the contents of location 200H into register AL will transfer the contents of location 200H and 201H into BX put the hexadecimal 200H in AX copy the contents of register CL into location 200H copy the contents of register DX into location 200H and 201H
Address 200H 201H 202H Mem Loc 8B 2A

Example:
AH AL 8B

MOV AX,2A8B MOV [200H],AX

AX

2A 2A

CH

CL 8B

MOV CX,[200H]

CX

2A

BH

BL 00 DH DL 8B

MOV BX,200H MOV DX,[BX]

BX

02

DX

2A

Simulation:
MEMORY LOCATION

MOV MOV MOV MOV MOV

AX,2A8B [200H],AX CX,[200H] BX,200H DX,[BX]

AH 2A 2A 2A 2A 2A

AL BH BL CH CL DH DL 8B 8B 8B 2A 8B 8B 02 00 2A 8B 8B 02 00 2A 8B 2A 8B

200H 201H 8B 8B 8B 8B 2A 2A 2A 2A

UST Faculty of Engineering

J.A.Agbayani

Page 21

IT103 Exercise 6 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

What will be the contents of locations 200H, 201H, 202H and 203H after execution of each of the following program fragments (use ? to describe a value which is unknown): REGISTERS BX CX MEMORY LOCATIONS 200H 201H 202H 203H

Program 1

AX

DX

MOV MOV MOV MOV MOV

AL,6 AH,5 BX,0ABDCH [200H],AX [202H],BX


AX REGISTERS BX CX DX MEMORY LOCATIONS 200H 201H 202H 203H

Program 2

MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV

AX,43BCH BX,0FE01H [200H],AX [202H],BX AL,[201H] BL,[203H] CL,BL BL,AL AL,CL [201H],AL [203H],BL
AX REGISTERS BX CX DX MEMORY LOCATIONS 200H 201H 202H 203H

Program 3

MOV MOV MOV MOV MOV ADD MOV

CX,34H [201H],CX DX,808H [202H],DX AX,[201H] AX,14H [200H],AX

UST Faculty of Engineering

J.A.Agbayani

Page 22

;input1.ASM - Input a string ;the ASCII codes for the characters entered are saved in a buffer called keys, which is limited to 128 characters.
cout MACRO param1 MOV DX,OFFSET param1 MOV AH,9 INT 21H ENDM .MODEL small 64 DB "Type a string: $" DB "You typed: $" DB 128 DUP(?) .CODE PROC FAR MOV AX,@DATA MOV DS,AX MOV AX,3H INT 10H cout prompt1 MOV MOV MOV INT INC MOV INC CMP JNE CX,0 DI,OFFSET keys AH,1H 21H CX [DI],AL DI AL,0DH inp ;cin>>keys; ;count number of characters typed ;DI points to start of buffer keys ;press a character ;character stored to AL ;save character in buffer keys ;point to next buffer location ;continue until ENTER key is seen ;call the newline procedure ;cout<<keys; ;DI points to start of buffer keys ;copy the content to DL ;display it ;point to next buffer location ;loop while CX != 0 ;call the newline procedure ;exit to DOS ;clrscr(); ;define MACRO

;end MACRO

.STACK .DATA prompt1 prompt2 keys main

inp:

CALL newline cout prompt2 outp: MOV DI,OFFSET keys MOV DL,[DI] MOV AH,2 INT 21H INC DI LOOP outp CALL newline MOV AH,4CH INT 21H ENDP PROC NEAR MOV DL,10 MOV AH,2 INT 21H MOV DL,13 INT 21H RET ENDP END main

main newline

;all subprocedures ends with RET ;end of program

newline

UST Faculty of Engineering

J.A.Agbayani

Page 23

UST Faculty of Engineering

J.A.Agbayani

Page 24

IT103 Exercise 7 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

Write a complete assembly language program that asks a user to input a number, examine all the digits and tell whether the number of even digits is equal, greater or smaller than the number of odd digits. Sample screen dialogue: G:\>input <enter> Input a number: 9078542 <enter> Greater. G:\>input <enter> Input a number: 8723309 <enter> Smaller. G:\>input <enter> Input a number: 1234567890 <enter> Equal. G:\>_

UST Faculty of Engineering

J.A.Agbayani

Page 25

;Input String Function - MOV AH,0AH


cout MACRO param1 MOV DX,OFFSET param1 MOV AH,9 INT 21H ENDM ;define macro

;end macro

prompt1 prompt2 prompt3 newline myName main

.MODEL SMALL .STACK 64 .DATA DB 13,10,"Type your name: $" DB 13,10,"Hello $" DB 13,10,"More? $" DB 13,10,"$" DB 41 .CODE PROC FAR MOV AX,@DATA MOV DS,AX MOV AX,3H INT 10H cout prompt1 MOV DX,OFFSET myName MOV AH,0AH INT 21H cout newline cout prompt2 MOV DI,OFFSET myName+1 MOV CH,0 MOV CL,[DI] MOV DI,OFFSET myName+2 MOV DL,[DI] MOV AH,2 INT 21H INC DI LOOP outp cout prompt3 MOV AH,1 INT 21H CMP AL,'Y' JE again CMP AL,'y' JE again MOV AH,4CH INT 21H ENDP END main ;exit to dos ;clrscr() ;Type your name: ;cin>>myName

again:

;\n ;Hello ;get length of myName ;put the string lenght to CX ;get the first character ;print each char of myName

outp:

;More? ;input a char

main

UST Faculty of Engineering

J.A.Agbayani

Page 26

UST Faculty of Engineering

J.A.Agbayani

Page 27

IT103 Exercise 8 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

1. Write a program that asks for a whole number and swap every two digits in this value. Ex. If n = 12345, display 21435 If n = 811230, display 182103 If n = 10101, display 01011 Sample screen dialogue: G:\>input <enter> Input a number: 12345 <enter> 21435 G:\>_

2. Enter characters of the string in ascending order, eliminate characters with duplicates and display back the string in unique character sequence. Sample screen dialogue: G:\>input <enter> G:\>input <enter> Enter characters in ascending order aa bb c dd eee mm <enter> Eliminating duplicates a b c d e m G:\>_

UST Faculty of Engineering

J.A.Agbayani

Page 28

STACK A stack is a group of locations in memory which the programmer reserves for the (temporary) storage of important items of data. The mechanism by which subroutines are implemented in the 8086 family uses a stack. Briefly, when a subroutine is called up, the address of the instruction following the call is saved on a stack until the RET instruction in the body of the subroutine is encountered. Then execution continues from the instruction following the call by retrieving the return address from that stack. Copies of the contents of registers can be stored on and retrieved from a stack with the use of instructions PUSH and POP.

PUSH <16-bit register name>

for example, PUSH AX

POP <16-bit register name> for example, POP BX PUSH stores the copy in memory, POP loads it back again. PUSH AX PUSH BX POP AX POP BX ;copy AX onto stack ;copy BX onto stack ;copy the item currently on the top of ;register AX and then remove that item ;copy the item currently on the top of ;register BX and then remove that item ;the stack

the stack into from the stack the stack into from the top of

The top of the stack is where items are added and removed. In order that PUSH and POP can operate successfully it is necessary only to: specify how big a memory stack is required; set the stack segment register SS to the segment address of the start of the stack; and set the 16-bit STACK POINTER (SP) register to the offset address of the top of the stack. Both SS and SP are initialized automatically by TASM and TLINK. Let us suppose that the stack pointer register, SP, has been initialized to 0200H (relative to the SS register) and consider the effect of the instructions PUSH BX in detail. Suppose further that register BX contains 1A9EH. Then the action of PUSH BX is as follows: 1. The SP register is decremented by 1. 2. The high-order byte of BX is stored in the memory location addressed by SP (relative to the SS register). 3. The SP register is decremented by 1. 4. The low-order byte of BX is stored in the memory location now addressed by SP (relative to the SS register). On the other hand, the action of an instruction such as POP DX releases memory from the stack, in that the SP register is incremented. Its action is as follows: 1. Copy the byte stored at the address given in the SP register (relative to SS) into DL. 2. Increment the SP register by 1. 3. Copy the byte now pointed to by the SP register (relative to SS) into DH. 4. Increment the SP register by 1.

UST Faculty of Engineering

J.A.Agbayani

Page 29

Example: Make a detailed trace of the execution of the following program fragment, giving the contents of all the registers and memory locations involved. Assume that before execution begins the SS register contains 1000H, and the SP register 0200H, and that the contents of AX, BX, CX and DX are (respectively) 0100H, 123AH, 0FE1BH and 0A981H. AX 0100 PUSH AX BX 123A CX FEIB DX A981

PUSH BX
PUSH CX POP DX POP CX PUSH DX

POP AX POP BX

UST Faculty of Engineering

J.A.Agbayani

Page 30

;FILENAME: input.ASM - Input a string, display its reverse. ;The ASCII codes for the characters entered are pushed into stack cout MACRO param1 MOV DX,OFFSET param1 MOV AH,9 INT 21H ENDM ;define MACRO

;end MACRO

.MODEL small .STACK 64 .DATA prompt1 DB "Type a string: $" prompt2 DB 13,10,"Reverse is: ",13,10,'$' newline DB 13,10,'$' .CODE main PROC FAR MOV AX,@DATA MOV DS,AX MOV AX,3H INT 10H cout prompt1 MOV CX,0 inp: MOV AH,1H INT 21H INC CX PUSH AX CMP AL,13 JNE inp cout prompt2 outp: POP DX MOV AH,2 INT 21H LOOP outp cout newline MOV AH,4CH INT 21H main ENDP END main ;clrscr();

;count number of characters ;press a character ;character stored to AL ;increment counter ;save character into stack ;continue until ENTER key is ;seen ;copy top of the stack to DX ;display it ;loop while CX != 0 ;call the newline procedure ;exit to DOS ;end of program

UST Faculty of Engineering

J.A.Agbayani

Page 31

IT103 Exercise 9 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________ CX CL E

Registers AX, BX and CX all contain the ASCII codes for two letters as follows AX BX AH AL BH BL CH S O R T M

Using PUSH, POP and MOV, write a subroutine which leaves AX, BX and CX containing the letters in alphabetical order: AX BX CX AH AL BH BL CH CL E M O R S T

UST Faculty of Engineering

J.A.Agbayani

Page 32

ARITHMETIC FLAGS AND OPERATIONS The Carry Flag (CF) CY=1 NC=0 -if an addition has produced a carry then CF is set to 1, otherwise it is set to 0; -if a subtraction has produced a borrow then CF is set to 1, otherwise it is set to 0; The Overflow Flag (OF) OV=1 NV=0 -is set to 1 if adding two like-signed numbers or subtracting two opposite-signed numbers gives a result which requires more bits for an accurate representation than the operands themselves, otherwise it is set to 0 The Sign Flag (SF) NG=1 PL=0 -it indicates whether the result of an operation on signed numbers is positive (in which case the sign flag is set to 0) or negative (in which case it is set to 1). The Zero Flag (ZF) ZR=1 NZ=0 -if the result of an operation is zero, ZF is set to 1, otherwise it is set to 0 The Parity Flag (PF) PE=1 PO=0 -simply records whether the result of an operation contains an even number of 1s (in which case the parity flag is set to 1) or an odd number of 1s (in which case the parity flag is set to 0). Example: Make a trace of the values taken by the flags as the program is executed. AL BL DH CF OF SF ZF MOV BL,3 ADD BL,2 SUB BL,5 SUB BL,2 MOV DH,2 ADD DH,0FFH MOV AL,4 ADD AL,7FH PF

UST Faculty of Engineering

J.A.Agbayani

Page 33

IT103 Exercise 10 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

Make a trace of the values taken by the flags as the program is executed. AL (1) MOV SUB SUB SUB SUB CL,1 CL,65H CL,21H CL,86H CL,33H AX (2) MOV AX,0FEH INC AX MOV BX,0 ADD BX,AX DEC BX SUB AX,BX INC AX INC BX SUB AX,BX DEC AX ADD BX,BX SUB AX,BX INC BX INC AX BX CF OF SF ZF PF BL DH CF OF SF ZF PF

UST Faculty of Engineering

J.A.Agbayani

Page 34

80x86 Instructions: Arithmethic, Logical and Bit Manipulation ADC destination,source The operation of ADC is similar to ADD; however, in addition to the source operand the processor also adds the contents of the carry flag. The carry flag is then updated based on the size of the result. Other flags are also affected.

SBB destination,source SBB executes in much the same ways as SUB, except the contents of the carry flag are also subtracted from the destination oeprand. The contents of the carry flag are updated at completion of the instuction. Operating on a value in memory byte ptr directive What is the result of DEC byte ptr[200H]? Assume that the DS register contains 0500H. Solution: Because we are decrementing the contents of memory location, the assembler must be informed of the operand size. This is accomplished with the byte ptr directive.

UST Faculty of Engineering

J.A.Agbayani

Page 35

NEG destination This instruction is used to find the signed 2s complement representation of the number in the destination. This is accomplished by subtracting the destination operand from 0. All flags are affected. AX MOV AX,FFECH NEG AX CBW (Convert byte to word) This instruction is used to extend a signed 8-bit number in AL into signed 16-bit number in AX. No flags are affected. AL AX MOV AL,37H CWD CWD (Convert word to double-word) This instruction extends the sign of the number stored in AX through all 16 bits of register DX. This results in a 32-bit signed number in DX and AX. No flags are affected. AX DX MOV AX,4000H CWD

UST Faculty of Engineering

J.A.Agbayani

Page 36

Logical Instructions (NOT, OR and AND) NOT destination This instruction finds the complement of the binary data stored in the destination operand. All 0s are changed to 1s, and all 1s to 0s. No flags are affected. AL MOV AL,3EH NOT AL AND destination,source This instruction performs the logical AND operation on the destination and source operands. AND is useful way of turning bits off (making them 0). All flags are affected. AL MOV AL,BCH AND AL,27H OR destination,source This instruction performs a logical OR of each bit in the source and destination operands. OR can be used to turn bits on (make them 1). All flags are affected. BL MOV BL,89H OR BL,72H XOR destination,source XOR is a special form of OR in which the inputs must be different to get a 1 out of the logic gate. All flags are affected. AL CL MOV AL,12H MOV CL,45H XOR AL,CL

UST Faculty of Engineering

J.A.Agbayani

Page 37

Bit Manipulation SHL destination,count All bits in the destination operand are shifted left, with a 0 entering the LSB (Least Significant Bit) each time. Bits that shift out of the MSB (Most Significant Bit) position are placed into the carry flag. The count operand indicates how many bits the destination operand is to be shifted to the left. AL CL CF MOV AL,75H MOV CL,3 SHL AL,CL

SHR destination,count This instruction has the opposite effect of SHL, with bits shifting to the right in the destination operand. Zeros are shifted in from the left and the bits that fall out of the LSB positon end up in carry flag. Keep in mind that shifting right 1 bit is equivalent to dividing by 2. AL CF MOV AL,75 SHR AL,1 ROL destination,count The difference between ROL and SHL is that bits that get rotated out of the LSB position get rotated back into the MSB. Thus, data inside the destination operand is never lost, only circulated within itself. A copy of the bit that rotates out of the LSB is placed into the carry flag. The only other flag affected is the overlfow flag. AL CF MOV AL,41H ROL AL,1

UST Faculty of Engineering

J.A.Agbayani

Page 38

ROR destination,count This instruction has the opposite effect of ROL, with bits moving to the right within the destination operand. The bits that rotates out of the LSB goes into the carry flag and also into the MSB. AX CF MOV AX,3E95H ROR AX,2

UST Faculty of Engineering

J.A.Agbayani

Page 39

IT103 Exercise 11 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

Make traces of the execution of the following program fragment. Program 1 MOV AL,38H MOV BL,2AH MOV CL,0ABH ADD AL,CL ADC BL,CL NOT CL OR CL,BL SBB CL,D3H SHL BL,CL XOR BL,AL ROR BL,CL AND AL,BL NEG AL Program 2 MOV AX,1C71H MOV BX,E38FH AND AX,BX NOT AX OR AX,BX NOT BX JZ N AND BX,AX NOT BX JMP E N: AND AX,BX E: NOT AX AL BL CL CF OF SF

AX

BX

UST Faculty of Engineering

J.A.Agbayani

Page 40

DISK ORGANIZATION
The hard disk drive in your system is the "data center" of the PC. It is here that all of your programs and data are stored between the occasions that you use the computer. Your hard disk (or disks) are the most important of the various types of permanent storage used in PCs (the others being floppy disks and other storage media such as CD-ROMs, tapes, removable drives, etc.) The hard disk differs from the others primarily in three ways: size (usually larger), speed (usually faster) and permanence (usually fixed in the PC and not removable). For processing records on disks, you need some familiarity with the terms and characteristics of disk organization. A diskette has two sides (or surfaces), whereas a hard disk contains a number of two-sided disks on a spindle. Tracks and Sectors Each side of the diskette or hard disk contains a number of concentric tracks, numbered beginning with 00, the outermost track. Each track is formatted into sectors of 512 bytes, where the data is stored. Both diskettes and hard disk devices are run by a controller that handles the placement of the read-write heads on the disk surface and the transfer of data between disk and memory. Cylinders A cylinder is a vertical set of all of the tracks with the same number on each surface of a diskette or hard disk. Thus cylinder 0 is the set of all tracks numbered 0 on every side, cylinder 1 is the set of all tracks numbered 1, and so forth. When writing a file, the controller fills all the tracks on a cylinder and then advances a read-write heads to the next cylinder Disk Controller The disk controller, which is located between the processor and the disk drive, handles all communication between them. The controller accepts data from the processor and converts the data into a form that is usable by the device. Clusters A cluster is a group of sectors that the system treats as a unit of storage space. A cluster size is always a power of 2, such as 1, 2, 4, or 8 sectors. On a disk device that supports one sector per cluster, sector and cluster are the same. A hard disk typically has four sectors per cluster. A file begins on a cluster boundary and requires minimum of one cluster, even if the file occupies only one of the four sectors. A cluster may also overlap from one track to another.

UST Faculty of Engineering

J.A.Agbayani

Page 41

The BIOS (Basic Input/Output System) The BIOS and operating system play an important role in how your hard disk is used. While the BIOS itself has taken more of a "back seat" role to direct access by the operating system over the last few years, it is still there "in the mix" in several ways. The INT 13H Interface When the operating system or an application wants to access the hard disk, it traditionally employs BIOS services to do this. The primary interface to the BIOS has been the software interrupt known as INT 13H, where "INT" stands for interrupt and "13H" is the number 19 in hexadecimal notation. The INT 13H interface supports many different commands that can be given to the BIOS, which then passes them on to the hard disk. These include most anything that you would normally want to do with a disk--reading, writing, formatting, and so on.

UST Faculty of Engineering

J.A.Agbayani

Page 42

Disk Calculations Consider this disk with the following characteristics: o 90 surfaces o 200 tracks/surface o 50 sectors/track o 512 bytes/sector 1. How many cylinders are there on this disk?

2. How many sectors are there on this disk?

3. What is the capacity of this disk in Megabytes? (1Megabyte = 220 bytes = 1,048,576 bytes)

4. How many Megabytes are there in 80 cylinders?

5. How many files of 250 bytes each can be stored on this disk assuming that no file split across the sector boundary?

UST Faculty of Engineering

J.A.Agbayani

Page 43

IT103 Exercise 12 UST Faculty of Engineering

Name: ___________________________ Section__________ Date: ____________________

SCSI Hard Disk has 500 cylinders of 75 tracks each. Each track has 30 sectors of 4,096 bits each. Show complete solution. 1. What is the capacity of this disk in Megabytes?

2. What is the capacity of 50 surfaces in Megabytes?

3. How many sectors are there in 50 surfaces?

4. How many cylinders are required to store a total of 1 Megabyte file.

5. How many files of 250 bytes each can be stored on this disk assuming that no file split across the sector boundary?

UST Faculty of Engineering

J.A.Agbayani

Page 44

ASCII Table

UST Faculty of Engineering

J.A.Agbayani

Page 45

Assembly Language Programs


;PROGRAM1.asm - Prototype of an Assembly Language Program .MODEL SMALL .STACK 64 .DATA .CODE main PROC FAR .............. .............. .............. .............. .............. main ENDP END main ;stop the main procedure _____________________________________________________________________ ;PROGRAM2.asm More than one procedure .MODEL SMALL .STACK 64 .DATA .CODE PROC FAR .............. .............. CALL subp .............. .............. ENDP

main

;call a sub-procedure called subp

main subp

PROC NEAR .............. .............. .............. .............. ;all sub-procedures RET ;should end with RET subp ENDP END main _____________________________________________________________________ ;PROGRAM3.asm 1st two and Last two instructions inside ;the main procedure .MODEL SMALL .STACK 64 .DATA .CODE main PROC FAR MOV AX,@DATA ;get the address of DATA segment MOV DS,AX ............... ............... ............... MOV AH,4CH ;exit to DOS INT 21H main ENDP
UST Faculty of Engineering J.A.Agbayani Page 46

END main ;PROGRAM4.asm - Input a character .MODEL SMALL .STACK .DATA .CODE PROC FAR MOV AX,3 INT 10H MOV AH,1 INT 21H MOV AH,4CH INT 21H ENDP END main

main

;clrscr() ;input a character ;character typed stored to AL ;exit to DOS

main

______________________________________________________________________ ;PROGRAM5.asm - Output a character .MODEL SMALL .STACK .DATA .CODE PROC FAR MOV AX,3 INT 10H MOV DL,65 MOV AH,2 INT 21H INC DL MOV AH,2 INT 21H MOV AH,4CH INT 21H ENDP END main

main

;clrscr() ;ASCII code of 'A' ;output a character ;'B' ;display it ;exit to DOS

main

Screen Output:

UST Faculty of Engineering

J.A.Agbayani

Page 47

;PROGRAM 6. Output a string .MODEL SMALL .STACK .DATA DB 13,10,"Jess Agbayani$" .CODE PROC FAR MOV AX,@DATA ;get the address of DATA segment MOV DS,AX MOV AX,3 INT 10H MOV DX,OFFSET myname MOV AH,9 INT 21H MOV AH,4CH INT 21H ENDP END main ;clrscr() ;LEA DX,name ;output a STRING ;exit to DOS

myname main

main

Screen Output:

UST Faculty of Engineering

J.A.Agbayani

Page 48

;PROGRAM 7. Output a string .MODEL SMALL .DATA DB 13,10,Name: Jess Agbayani DB 13,10,Address: Deparo, Caloocan City DB 13,10,Nationality: Filipino DB 13,10,Gender: Male$ DB 13,10,$ .CODE PROC FAR MOV AX,@DATA ;get the address of DATA segment MOV DS,AX MOV AX,3 INT 10H MOV DX,OFFSET myname MOV AH,9 INT 21H MOV DX,OFFSET nline INT 21H MOV AH,4CH INT 21H ENDP END main ;clrscr()

myname

nline main

;output a STRING ;new line ;exit to DOS

main

Screen Output:

UST Faculty of Engineering

J.A.Agbayani

Page 49

;PROGRAM 8. Input/Output a Character ;Filename: program8.asm .MODEL small .STACK 64 .DATA prompt1 DB "Input a character: $" prompt2 DB 0AH,0DH,"You typed: $" prompt3 DB 0AH,0DH,"Want More? $" .CODE main PROC FAR MOV AX,@data MOV DS,AX start: MOV AX,3 INT 10H MOV DX,OFFSET prompt1 MOV AH,9 INT 21H MOV AH,1 INT 21H MOV BL,AL MOV DX,OFFSET prompt2 MOV AH,9 INT 21H MOV DL,BL MOV AH,2 INT 21H MOV DX,OFFSET prompt3 MOV AH,9 INT 21H MOV AH,1 INT 21H CMP AL,'Y' JE start CMP AL,'y' JE start MOV AH,4CH INT 21H ENDP END main ;clrscr();

;cout<<prompt1; ;press a character ;store character to BL ;cout<<prompt2

;display character ;Do you want more?

;press any key

;jump if equal to start label

;exit to DOS ;end of program

main

Program OUTPUT: Input a character: A <Enter> You typed A Want more? Y

UST Faculty of Engineering

J.A.Agbayani

Page 50

Das könnte Ihnen auch gefallen