Solution Manual For Assembly Language Programming and Organization of IBM PC PDF
Solution Manual For Assembly Language Programming and Organization of IBM PC PDF
Assembly Language
Programming and
Organization of the
IBM PC
By Yu and Marut
Manual solution
Ch.1 to ch.10
Written by :
Eng. Amer Mohammed Al-
Al-khsabah
VIPER-
VIPER-VIRUS@LIVE.COM
https://pucit-online.blogspot.com
2
Contents
Preface …………………………………………………………………. 1
Ch.1 …………………………………………………………………. 2
Ch.2 …………………………………………………………………. 7
Ch.3 …………………………………………………………………. 14
Ch.4 …………………………………………………………………. 17
Ch.5 …………………………………………………………………. 32
Ch.6 …………………………………………………………………. 37
Ch.7 …………………………………………………………………. 58
Ch.8 …………………………………………………………………. 84
Ch.9 …………………………………………………………………. 93
Ch.10 …………………………………………………………………. 110
Appendix A (How to run a program)………………… .. 112
Appendix B ( Some useful procedure)………………… 117
https://pucit-online.blogspot.com
3
Chapter One
Microcomputer Systems
(1)
a)
• 1111111100010001b
• 0101010111111111b
• 0001000111011101b
b)
• 0
• 1
• 1
• 1
(2)
a) 1101
b) 0101
(3)
a) RAM
b) ROM
c) RAM
d) ROM
https://pucit-online.blogspot.com
4
Chapter One
(4)
a) the microprocessor :
brain of the computer , it controls the computer by executing
programs stored in memory.
b) the buses:
connect the different component to make processor
communicate with memory and I/O circuits.
(5)
a) EU (Execution Unit ) :
execute instructions – because it contains the ALU.
(6)
a) IP : contains the address of the next instruction to be executed
by EU .
https://pucit-online.blogspot.com
5
Chapter One
(7)
a) I/O ports : transfer points between the CPU & I/O devices
(8)
4-Bytes
(9)
a)
• Fetch an instruction from memory.
• Decode the instruction to determine the operation.
b)
• Perform the operation if needed .
• Store the results in memory.
(10)
a) Advantages of H.L.L:
1. closer to natural language , so it’s easier to read and understand
the program .
2. assembly contains more statement than an equivalent HLL ,so
It’s need more time to code the assembly language program.
https://pucit-online.blogspot.com
6
Chapter One
https://pucit-online.blogspot.com
7
Chapter Two
(1)
Binary Decimal Hex
1001 9 9
1010 10 A
1101 13 D
1100 12 C
1110 14 E
1011 11 B
(2)
a) 14d d: decimal
b) 2397d
c) 1130d
d) 1027628d
(3)
a) 1100001b b: binary
b) 1001110011b
c) 399h h: hexadecimal
d) 17E8h
(4)
a) 4Bh
b) 95AEh
c) 101000101100b
d) 1011001101001101b
https://pucit-online.blogspot.com
8
Chapter Two
(5)
a) 111100b
b) 10110110110b
c) C9CDFh
d) 1FACABh
(6)
a) 101b
b) 1001010b
c) 25041h
d) D00DFh
(7)
a) 234d = 0000000011101010b = 00Eah
b) 16d = 00000000010000b
-16d = 2’s comp. (16)
= 1111111111110000b = FFF0h
d) 32216d = 0111110111011000b
-32216d = 2’s comp.(32216)
= 1000001000101000b = 8228h
Note: 2’s comp. =1’s comp +1
1’s comp. =NOT
https://pucit-online.blogspot.com
9
Chapter Two
(8)
Take 2’s comp. for negative number ,then Add the two
numbers.,if no.1> no.2 : positive result stay as it.
If no.1< no.2 :negative take 2’s comp.
https://pucit-online.blogspot.com
10
Chapter Two
(9)
Unsigned :convert the number as it is.
Signed :if msb=0 convert the number as it is.
If msb=1 take 2’s comp. then convert.
Unsigned signed
a) 7FFEh 32766d 32766d
b) 8543h 34115d -31421d
c) FEh 254d -2d
d) 7Fh 127d 127d
(10)
120d = 01111000b 8-bit
0000000001111000b 16-bit
To make it negative take 2’s comp.
-120d = 10001000b 8-bit
1111111110001000b 16-bit
a) 10001000b
b) 1111111110001000b
https://pucit-online.blogspot.com
11
Chapter Two
(11)
16-bit 8-bit
a) 32767d 111111111111111b X :too big
b) -40000d X :too big X :too big
c) 65536d X :too big X :too big
d) 257d 0000000100000001b X :too big
e) -128 0000000010000000b 10000000b
(12)
If msb =0 positive else negative
a) 1010010010010100b Negative
b) 78E3h = 0111100011010011b Positive
c) CB33h = 1100101100110011b Negative
d) 807Fh = 1000000001111111b Negative
e) 9AC4h = 1001101011000100b Negative
https://pucit-online.blogspot.com
12
Chapter Two
(13)
Address Content(hex) Data
0 24 $
1 31 1
2 32 2
3 2E .
4 37 7
5 35 5
(14)
41 74 74 61 63 6B 20 61 74 20 44 61
A t t a c k a t D a
77 6E
w n
“Attack at Dawn”
(15)
A = 41h a = 61h
The difference is 20h
Add 20h to convert from upper to lower case.
https://pucit-online.blogspot.com
13
Chapter Two
(16)
“0” = 30h numerical value = 0h
“1” = 31h numerical value = 1h
The difference is 30h
SUB 30h to get the numerical value.
(17)
Just a technique:
0 1 2 3 4 5 6 7 8 9 A B C D E F …….->
5(c) B23CDh
+ 1791 2h
D+2= go from D to the right 2 places = F
C+1= go from C to the right 1 place = D
3+9= go from 3 to the right 9 places = C
2+7= go from 2 to the right 7 places = 9
B+1= go from B to the right 1 place = C
= C9CDFh
https://pucit-online.blogspot.com
14
Chapter Three
(1)
8086 & 80286 have 16-bit but 80286 has this advances:
• Tow mode of operation.
• More addressable memory.
• Virtual memory in protected mode.
(2)
Registers M.L
Inside CPU in RAM
Perform operation store data
Known as name known by Address
14-reg 16-bit 1MB 8-bit
(3)
https://pucit-online.blogspot.com
15
Chapter Three
(4)
0A51:CD90h = seg:offset
Physical address = seg × 10h +offset
= 0A51 × 10h + CD90h
= 0A510 + CD90h
= 172A0h
(5)
Physical Address = 4A37Bh
a) seg. = 40FFh
Physical address = seg × 10h + offset
Offset = physical address – seg × 10h
= 4A37B – 40FF × 10h
= 4A37B – 40FF0
= 938Bh
40FF:938Bh
b) offset = 123Bh
Physical address = seg × 10 + offset
Seg × 10h = physical address – offset
Seg × 10h = 4A37B – 123B
Seg × 10h = 49140
Seg = 49140/10h
= 4914h
4914: 123Bh
https://pucit-online.blogspot.com
16
Chapter Three
(6)
Paragraph = 16 byte
So any address accept division on 16d /10h call paragraph
boundary.
(7)
The compatibility of PC clones with the IBM PC depends
on how well their BIOS routines match thoses of the IBM
PC.
(8)
That depends on the size of RAM used.
For 8086 1MB is used 10-segment to loading and running
applications , these 10-segment give us 640 KB of memory.
https://pucit-online.blogspot.com
17
Chapter Four
(1)
a) legal
b) legal
c) illegal ; contains a blank (space)
d) legal
e) legal
f) illegal ; contains an illegal character ‘
g) illegal ; contains an illegal character =
(2)
a) 246 ; legal – decimal
b) 246h ; legal – hexa
c) 1001 ; legal – decimal
d) 1,101 ; illegal – nondigit character ,
e) 2A3h ; legal – hexa
f) FFFEh ; illegal – must begin with digit
g) 0Ah ; legal – hexa
h) Bh ; illegal – must begin with digit
i) 1110b ; legal – binary
(3)
a) A DW 52
b) WORD1 DW ?
c) B DB 25h
d) C1 DB ?
e) illegal , number too big can’t fit in word
f) ARRAY1 DW 1,2,3,4,5
https://pucit-online.blogspot.com
18
Chapter Four
(4)
Address content beginning of var.
0000h 7 A
0001h BCh B
0002h 1Ah
0003h ‘H’ C
0004h ‘E’
0005h ‘L’
0006h ‘L’
0007h ‘O’
a) var. A : 0000h
var. B : 0001h
var. C : 0003h
https://pucit-online.blogspot.com
19
Chapter Four
(5)
a) legal
b) illegal ; constant to segment
c) illegal ; segment to segment
d) legal
e) illegal ; memory to memory
f) illgal ; can’t store in constant
g) illegal ; memory to memory operation
h) illegal ; out of range can’t fit in 8-bit
i) illegal ; momory to memory / different size
(6)
a) MOV AX,B
SUB AX,A
MOV A,AX
b) MOV AX,A
INC AX
NEG AX
MOV A,AX
c) MOV AX,A
ADD AX,B
MOV C,AX
https://pucit-online.blogspot.com
20
Chapter Four
d) MOV AX,B
ADD AX,B
ADD AX,B
ADD AX,7
MOV B,AX
e) MOV AX,B
SUB AX,A
DEC AX
MOV A,AX
(7)
a) MOV AH,1
INT 21H
MOV DL,AL
MOV AH,2
INT 21H
b) MOV AH,1
INT 21H
ADD AL,20H ;convert to lower case
MOV DL,AL
MOV AH,2
INT 21H
https://pucit-online.blogspot.com
21
Chapter Four
Programming Exercises
(8)
.MODEL SMALL
.DATA
MSG DB 0AH,0DH,'THE SUM OF '
C1 DB ?,' AND '
C2 DB ?,' IS '
SUM DB ?,'$'
.CODE
Main PROC
MOV AH,1
INT 21H ; read 1st digit
MOV C1,AL ; store it in memory
https://pucit-online.blogspot.com
22
Chapter Four
Main ENDP
END Main
https://pucit-online.blogspot.com
23
Chapter Four
(9)
.MODEL SMALL
.DATA
MSG DB 0AH,0DH,"ENTER THREE INITIALS: $"
C1 DB ?,0AH,0DH
C2 DB ?,0AH,0DH
C3 DB ?,'$'
.CODE
MAIN PROC
MOV AX,@DATA ;initialze DS
MOV DS,AX
https://pucit-online.blogspot.com
24
Chapter Four
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
25
Chapter Four
(10)
.MODEL SMALL
.DATA
MSG DB 0AH,0DH,'ENTER A HEX DIGIT: $'
MSG2 DB 0AH,0DH,'IN DECIMAL IT IS 1'
C1 DB ?,'$'
.CODE
MAIN PROC
MOV AX,@DATA ;initialize DS
MOV DS,AX
SUB AL,11H ;*
https://pucit-online.blogspot.com
26
Chapter Four
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
27
Chapter Four
(11)
.MODEL SMALL
.DATA
STARS DB 0AH,0DH,'**********','$'
.CODE
MAIN PROC
https://pucit-online.blogspot.com
28
Chapter Four
MAIN ENDP
END MAIN
MOV CX,10
L: INT 21H
LOOP L
https://pucit-online.blogspot.com
29
Chapter Four
(12)
.MODEL SMALL
.DATA
STARS DB ,'***********',0AH,0DH,'$'
MID DB ' *** '
C1 DB ?
C2 DB ?
C3 DB ?,' ***',0AH,0DH,'$'
.CODE
MAIN PROC
MOV AX,@DATA ; initialize DS
MOV DS,AX
https://pucit-online.blogspot.com
30
Chapter Four
https://pucit-online.blogspot.com
31
Chapter Four
https://pucit-online.blogspot.com
32
Chapter Five
(1)
Assuming that the flags are initially = 0 *
Content of CF SF ZF PF OF
a) AX= 8000h 0 1 0 1 1
b) AL= 02h 1 0 0 0 0
c) AL= FFh 0 1 0 1 0
d) AL= 81h 1 1 0 1 0
e) AX= 712Ah
BX= 1ABCh 0 0 0 0 0
f) AL= 7Fh 1 0 0 0 1
g) AX= 8000h 1 1 0 1 1
h) AX= FFFFh 1 1 0 1 0
https://pucit-online.blogspot.com
33
Chapter Five
b) cyout 1 0 cyinto
Cyinto =0 ,cyout =1
Cyinto XOR cyout = 0 XOR 1 = 1 = OF (signed overflow)
https://pucit-online.blogspot.com
34
Chapter Five
(3)
We’ll show solution of one and the rest are the same
a.
01
512Ch = 0101 0001 0010 1100
4185h = 0100 0001 1000 0101 +
92B1h = 1001 0010 1011 0001
Method 1:
By taking XOR between cyinto and cyout
Cyinto = 1 , cyout = 0 see Q2.a
1 XOR 0 = 1 OF = 1 (signed)
Cyout = CF = 0 (unsigned)
Method 2 :
First number is positive
Second number is positive
After addition the result expected is positive , but the result
is negative ,so there are error occurred
The error indicate by OF = 1
https://pucit-online.blogspot.com
35
Chapter Five
(4)
We’ll show solution of one and the rest are the same
c. borrow 1 0 borrow
into msb into bit-14
19BCh = 0001 1001 1011 1100
81FEh = 1000 0001 1111 1110 -
97BEh= 10001 0111 1011 1110
Method 1:
By taking XOR between borrowinto msb and borrowinto bit-14
borrowinto msb XOR borrowinto bit-14
1 XOR 0 = 1 OF = 1 (signed)
Method 2:
First number is positive
Second number is negative
But second number is greater than first number.
And 1st –(-2nd) = 1st + 2nd = positive.
The result is negative so an error occurred OF = 1
https://pucit-online.blogspot.com
36
Chapter Five
https://pucit-online.blogspot.com
37
Chapter Six
(1)
a) CMP AX,0
JGE END_IF
MOV BX,-1
END_IF:
b) CMP AL,0
JNL ELSE_
MOV AH,0FFh
JMP END_IF
ELSE_: MOV AH,0
END_IF:
OR
CMP AL,0
JL THEN_
MOV AH,0
JMP END_IF
THEN_: MOV AH,0FFh
END_IF:
c) CMP DL,’A’
JL END_IF
CMP DL,’Z’
JG END_IF
MOV AH,2 ; display DL
INT 21H
END_IF:
https://pucit-online.blogspot.com
38
Chapter Six
d) CMP AX,BX
JGE END_IF
CMP BX,CX
JGE ELSE_
MOV AX,0 ;then
JMP END_IF
e) CMP AX,BX
JL THEN_
CMP BX,CX
JL THEN_
MOV DX,1 ;else
JMP END_IF
f) CMP AX,BX
JNL ELSE_
MOV AX,0
JMP END_IF
https://pucit-online.blogspot.com
39
Chapter Six
MOV BX,0
JMP END_IF
OR
CMP AX,BX
JL THEN_
CMP BX,CX
JL THEN_2
MOV CX,0
JMP END_IF
END_IF:
https://pucit-online.blogspot.com
40
Chapter Six
(2)
MOV AH,1 ;read a character
INT 21H
END_CASE:
https://pucit-online.blogspot.com
41
Chapter Six
(3)
MOV CX,49
MOV AX,1 ;first term
MOV BX,1
LOOP L1
https://pucit-online.blogspot.com
42
Chapter Six
MOV CX,19
MOV AX,100
MOV BX,100
LOOP L1
(4)
a) MOV CX,50
MOV DX,1
MOV AX,1
L1:ADD AX,4
LOOP L1
https://pucit-online.blogspot.com
43
Chapter Six
b) MOV AH,1
INT 21H
MOV AH,2
MOV DL,0AH ;enter
INT 21H
MOV DL,0DH
INT 21H
MOV DL,AL
MOV CX,80
c) MOV CX,5
MOV AH,7 ;*
L1: INT 21H
LOOP L1
;----------------------------
MOV DL,'X'
MOV CX,5
MOV AH,2
L2: INT 21H
LOOP L2
https://pucit-online.blogspot.com
44
Chapter Six
(5)
MOV AX,0
;CX divident BX divisor AX quotient
while_:
CMP CX,BX
JL END_WHILE
INC AX
SUB CX,BX
JMP WHILE_
END_WHILE:
(6)
XOR CX,CX
; CX product BX,AX positive numbers
L1: ADD CX,AX
DEC BX
JNZ L1
https://pucit-online.blogspot.com
45
Chapter Six
(7)
a)
MOV AH,1
MOV CX,80
L1: INT 21H
CMP AL,20H ; blank = 20h
LOOPE L1
b)
MOV AH,1
MOV CX,80
L1: INT 21H
CMP AL,0DH
LOOPNE L1
;compare will set the flag ZF if equal 0DH and that affect
on LOOPNE .
https://pucit-online.blogspot.com
46
Chapter Six
Programming Exercises
(8)
.MODEL SMALL
.CODE
MAIN PROC
MOV AH,2
MOV DL,'?'
INT 21H ;display ‘?’
MOV AH,1
INT 21H ; read 1st char. & put it in BL
MOV BL,AL
INT 21H ;read 2nd char. in AL
;--------------------------
CMP BL,AL
JG SWITCH ;if not orderd
JMP DISPLAY
https://pucit-online.blogspot.com
47
Chapter Six
INT 21H
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
48
Chapter Six
(9)
.MODEL SMALL
.CODE
MAIN PROC
;BH counter (10 char per line)
MOV BH,0
MOV AH,2
MOV CX,7FH ; 127 char.
MOV DL,80H ;1ST letter
MOV BL,80H
https://pucit-online.blogspot.com
49
Chapter Six
JMP CONTINUE
Out_: MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
50
Sample execution for Q9
https://pucit-online.blogspot.com
51
Chapter Six
(10)
TITLE Q9:WRITTEN BY AMER AL-KHSABAH (V.V)
.MODEL SMALL
.STACK 64
;------------------------------------
.DATA
CR EQU 0DH
LF EQU 0AH
M1 DB CR,LF,'ENTER A HEXA DIGIT :','$'
M2 DB CR,LF,'IN DECIMAL IT IS :'
C1 DB ?,'$'
M3 DB CR,LF,'DO YOU WANT TO DO IT AGAIN ?’
DB ‘Y/N','$'
M4 DB CR,LF,'ILLEGAL CHARACTER - ENTER ‘
DB ‘0..9OR A..F :','$'
M5 DB CR,LF,'IN DECIMAL IT IS :1'
C2 DB ?,'$'
;-----------------------------------
.CODE
MAIN PROC
MOV AX,@DATA ;Initialize DS
MOV DS,AX
https://pucit-online.blogspot.com
52
Chapter Six
https://pucit-online.blogspot.com
53
Chapter Six
LEA DX,M5
INT 21H
;---------
JMP MSG
;--------
ILLEGAL2: MOV AH,9
LEA DX,M4
INT 21H
JMP NEW
ENDD: MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
54
Chapter Six
(11)
Same Q10 but we put error counter in illegal lable.
TITLE Q9:WRITTEN BY AMER AL-KHSABAH (V.V)
.MODEL SMALL
.STACK 64
;------------------------------------
.DATA
CR EQU 0DH
LF EQU 0AH
M1 DB CR,LF,'ENTER A HEXA DIGIT :','$'
M2 DB CR,LF,'IN DECIMAL IT IS :'
C1 DB ?,'$'
M3 DB CR,LF,'DO YOU WANT TO DO IT AGAIN ?’
DB ‘Y/N','$'
M4 DB CR,LF,'ILLEGAL CHARACTER – ENTER ‘
DB ‘0..9 OR A..F :','$'
M5 DB CR,LF,'IN DECIMAL IT IS :1'
C2 DB ?,'$'
M6 DB CR,LF,'ERROR THREE TIMES ENTERD’
DB ‘ILLEGAL CHAR !','$'
;-----------------------------------
.CODE
MAIN PROC
MOV AX,@DATA ;INITIALIZE DS
MOV DS,AX
;------
BEGIN: MOV BL,0 ;counter for error
MOV AH,9
LEA DX,M1
https://pucit-online.blogspot.com
55
Chapter Six
INT 21H
;--------
NEW: MOV AH,1 ;read hexa digit
INT 21H
;--------
CMP AL,'0' ;detictive for errors
JL ILLEGAL
CMP AL,'9'
JG ILLEGAL
;--------
MOV C1,AL
MOV AH,9
LEA DX,M2
INT 21H
;--------
MSG: MOV AH,9
LEA DX,M3
INT 21H
;---------
MOV AH,1 ;do u want to do it again
INT 21H
CMP AL,'Y'
JE BEGIN
CMP AL,'y'
JE BEGIN
JMP ENDD
;---------
ILLEGAL: CMP AL,'A'
JL ILLEGAL2
CMP AL,'F'
https://pucit-online.blogspot.com
56
Chapter Six
JG ILLEGAL2
SUB AL,11H
MOV C2,AL
;--------
MOV AH,9
LEA DX,M5
INT 21H
;---------
JMP MSG
;--------
ILLEGAL2: MOV AH,9
LEA DX,M4
INT 21H
; counter for 3-times illegal input
INC BL
CMP BL,3
JE ENDDD
JMP NEW
ENDDD: MOV AH,9
LEA DX,M6
INT 21H
https://pucit-online.blogspot.com
57
Chapter Six
https://pucit-online.blogspot.com
58
Chapter Seven
(1)
a) 10001011b
b) 11111001b
c) 10100110b
d) 10100001b
(2)
a) AND AX,AAAAh
b) OR BL,81h
c) XOR DX,8000h
d) method 1 : NOT WORD1
method 2 : XOR WORD1,FFFFh
(3)
a) TEST AX,FFFFh
b) TEST BX,0001h
c) TEST DX,8000h
d) TEST DX,8000h
e) TEST BL,FFh
https://pucit-online.blogspot.com
59
Chapter Seven
(4)
Assuming AL = 11001011b = CBh then
a) AL = 10010110b = 96h
b) AL = 01100101b = 65h
c) AL = 00101111b = 2Fh
d) AL = 01111001b = 79h
e) AL = 11110010b = F2h
f) AL = 10010111b = 97h
g) AL = 11111001b = F9h
(5)
a) SHL B5,1
b) MOV CL,3
SHL AL,CL
c) MOV AX,32142
MOV CL,2
SHR AX,CL
d) MOV BX,-2145
MOV CL,4
SAR BX,CL ;SAR to keep the sign
https://pucit-online.blogspot.com
60
Chapter Seven
(6)
a) OR AL,30h
b) OR DL,20h
(7)
a) MOV DL,BL
MOV CL,3
SHL BL,CL ; BL= 8BL
SHL DL,1 ; DL= 2BL
ADD BL,DL ; BL= 8BL+2BL =10BL
b) MOV CL,3
MOV AH,0 ;empty AH
ROR AX,CL ; remainder in AH
https://pucit-online.blogspot.com
61
Chapter Seven
Programming Exercises
(8)
.MODEL SMALL
.DATA
M1 DB 'TYPE A CHARACTER :','$'
M2 DB 0AH,0DH,'THE ASCII CODE OF '
C1 DB ?,' IN BINARY IS :','$'
M3 DB 0AH,0DH,'THE NUMBER OF 1 BIT IS '
C2 DB ?,'$'
.CODE
MAIN PROC
MOV AX,@DATA ;Initialize DS
MOV DS,AX
MOV BL,AL
MOV C1,AL ;store character
https://pucit-online.blogspot.com
62
LEA DX,M2
INT 21H
Chapter Seven
;-------------------
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
63
Chapter Seven
https://pucit-online.blogspot.com
64
Chapter Seven
(9)
.MODEL SMALL
.DATA
M1 DB 0AH,0DH,'TYPE A CHARACTER :','$'
M2 DB 0AH,0DH,'THE ASCII CODE OF '
C1 DB ?,' IN HEXA IS ','$'
.CODE
MAIN PROC
MOV AX,@DATA ;intialize DS
MOV DS,AX
;------------
BEGIN: MOV AH,9 ;prompt user
LEA DX,M1
INT 21h
https://pucit-online.blogspot.com
65
INT 21H
;-------------
Chapter Seven
MOV CL,4
SHR C1,CL ;prapare for display 1st half
;* note below
ADD C1,30H ;convert to char.
MOV DL,C1
JMP EXE1
https://pucit-online.blogspot.com
66
INT 21H
Chapter Seven
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
67
Chapter Seven
(10)
.MODEL SMALL
.DATA
M1 DB 0AH,0DH,'TYPE A HEXA NUMBER (0 - FFFF) : ','$'
M2 DB 0AH,0DH,'IN BINARY IT IS : ','$'
M3 DB 0AH,0DH,'ILLEGAL HEXA DIGIT, TRY AGAIN :','$'
.CODE
MAIN PROC
MOV AX,@DATA ;intialize DS
MOV DS,AX
https://pucit-online.blogspot.com
68
Chapter Seven
MOV DL,'0'
INT 21H
JMP LOOP1
https://pucit-online.blogspot.com
69
Chapter Seven
INT 21H
JMP START ;and read again
https://pucit-online.blogspot.com
70
Chapter Seven
(11)
.MODEL SMALL
.DATA
M1 DB 0AH,0DH,'TYPE A BINARY DIGIT ,UP TO 16-BIT: ','$'
M2 DB 0AH,0DH,'IN HEXA IT IS :','$'
.CODE
MAIN PROC
MOV AX,@DATA ;initialize DS
MOV DS,AX
https://pucit-online.blogspot.com
71
Chapter Seven
LETTER:
ADD DL,37H
INT 21H
L3: ROL BX,CL ;get second hexa digit
DEC DH
CMP DH,0
JNE L2 ;if 4-digit hexa displayed exit
https://pucit-online.blogspot.com
72
Chapter Seven
MOV AH,4CH ;return to DOS
INT 21H
MAIN ENDP
END MAIN
https://pucit-online.blogspot.com
73
Chapter Seven
(12)
.MODEL SMALL
.DATA
B1 DB ?
B2 DB ?
M1 DB 0AH,0DH,'TYPE A BINARY NUMBER , UP TO 8 DIGITS :','$'
M2 DB 0AH,0DH,'THE BINARY SUM IS ','$'
;--------------
.CODE
MAIN PROC
MOV AX,@DATA ;initialize DS
MOV DS,AX
MOV BL,0
MOV CX,8
MOV AH,1
https://pucit-online.blogspot.com
74
Chapter Seven
https://pucit-online.blogspot.com
75
Chapter Seven
INT 21H
continue: LOOP L3
;-----------------
https://pucit-online.blogspot.com
76
Chapter Seven
(13)
.MODEL SMALL
.DATA
M1 DB 0AH,'TYPE A HEXA NUMBER 0 -FFFF :$'
M2 DB 0AH,'THE SUM IN HEXA IS $'
COUNTER DB 4
NUM DW ?
.CODE
MAIN PROC
MOV AX,@DATA ;initialize DS
MOV DS,AX
MOV AH,9 ;prompt user
LEA DX,M1
INT 21H
CALL READ
MOV NUM,BX ;store 1st num.
https://pucit-online.blogspot.com
77
Chapter Seven
;-----------
ADD BX,NUM
;BX=num1(NUM)+num2(BX)
JC SHOWCY ;if there carry
MOV AH,2 ;no carry
MOV DL,'0'
INT 21H
JMP NEXT
SHOWCY:MOV AH,2
MOV DL,'1'
INT 21H
https://pucit-online.blogspot.com
78
Chapter Seven
;----------------------------------------
READ PROC ;for read hexa digit
XOR BX,BX
MOV CL,4
MOV AH,1
INT 21H
WHILE_: CMP AL,0DH
JE END_W
CMP AL,'9' ;detect for letter hexa
JG LETTER
AND AL,0FH
JMP SHIFT
https://pucit-online.blogspot.com
79
Chapter Seven
LETTER1:ADD DL,37H
https://pucit-online.blogspot.com
80
Chapter Seven
https://pucit-online.blogspot.com
81
Chapter Seven
(14)
Procedures are used
.MODEL SMALL
.DATA
M1 DB 0AH,'ENTER A DECIMAL DIGIT STRING :$'
M2 DB 0AH,'THE SUM OF THE DIGIT IN HEX IS $'
COUNTER DB 4
.CODE
MAIN PROC
MOV AX,@DATA ;initialize DS
MOV DS,AX
MOV AH,9 ;prompt user
LEA DX,M1
INT 21H
CALL READ
MOV AH,9
LEA DX,M2 ;display result msg
INT 21H
CALL SHOW
MAIN ENDP
https://pucit-online.blogspot.com
82
Chapter Seven
;------------------------------------------
READ PROC ;read decimal digit and add them
XOR BX,BX
WHILE_: MOV AH,1
INT 21H
CMP AL,0DH
JE END_W
AND AL,0FH
CBW ;Convert Byte to Word
;since we cann't add BX,AL we use CBW
ADD BX,AX
JMP WHILE_
END_W:
RET
READ ENDP
;----------------------------------------
SHOW PROC
MOV CL,4
START: MOV DL,BH
SHR DL,CL
CMP DL,9
JG LETTER1
ADD DL,30H
JMP SHOW1
LETTER1:ADD DL,37H
https://pucit-online.blogspot.com
83
INT 21H
Chapter Seven
ROL BX,CL
DEC COUNTER
CMP COUNTER,0
JNE START
RET
SHOW ENDP
END MAIN
https://pucit-online.blogspot.com
84
Chapter Eight
(1)
a) SP = 0100h
b) 100h byte for stack mean 100h/2 word
80h word
(2)
AX = 9ABCh
BX = 9ABCh
CX = 5678h
SP = 00FEh
(3)
When SP= 0 and decrement its become SP= FFFEh.
Program will not produce error ,but CS area is mix with SS
area ,that might produce error in result in big program.
(4)
IP = 0300h
SP = 0108h
Word on top of stack is 0203h
https://pucit-online.blogspot.com
85
Chapter Eight
(5)
a) IP = 012Ah
SP = 0202h
b) IP = 012Ah
SP = 0206h
(6)
a) POP AX
PUSH AX
b) POP AX
POP CX
PUSH CX
PUSH AX
c) POP AX
POP BX
PUSH AX
PUSH BX
https://pucit-online.blogspot.com
86
Chapter Eight
(7)
a) SAVE_REGS PROC
POP AX ;save return address
PUSH BX ;save regs in the stack
PUSH CX
PUSH DX
PUSH SI
PUSH DI
PUSH BP
PUSH DS
PUSH AX ;top of stack is return address
RET
SAVE_REGS ENDP
b) RESTORE_REGS PROC
POP AX ; save return address
POP DS ; restore regs from the stack
POP BP
POP DI
POP SI
POP DX
POP CX
POP BX
PUSH AX ; top of stack is return address
RET
RESTORE_REGS ENDP
https://pucit-online.blogspot.com
87
Chapter Eight
Programming Exercises
(10)
INBINARY PROC
XOR BX,BX
Mov CX,15
MOV AH,1
INT 21H
WHILE_: CMP AL,0DH
JE END_WHILE
AND AL,01H
SHL BX,1
OR BL,AL
INT 21H
LOOP WHILE_
END_WHILE:
RET
INBINARY ENDP
https://pucit-online.blogspot.com
88
Chapter Eight
b)
by following the algorithm:
RANDOM PROC
;input in AX : start with any number in the range
;output in AX (Random number)
PUSH BX ;save regs
PUSH DX
SHL AX,1 ; shift left once
MOV DX,AX ;get copy
SHL DX,1 ;make bit-14 be bit-15
XOR AX,DX ;xor bit-14 with bit-15
TEST DX,8000h ;result of XOR bit-14 with bit-15
JZ NOT_ONE
OR AX,1 ;replace bit-0 with result (1)
JMP CL_B15 ;clear bit-15
RANDOM ENDP
https://pucit-online.blogspot.com
89
Chapter Eight
c)
OUTBIN PROC
;input AX
;output on screen
;we copy number in BX to make sure not changing the
;number because of output function that depend on AX
MOV AH,2
MOV DL,'0'
INT 21H
JMP L1
RET
OUTBIN ENDP
https://pucit-online.blogspot.com
90
Chapter Eight
.CODE
Main PROC
MOV AX,@DATA
MOV DS,AX
;-------------------
CALL INBINARY
MOV AX,BX
Begin: MOV D,4
L10: POP AX
CALL RANDOM
CALL OUTBIN
PUSH AX
;-------------------
MOV AH,2
MOV DL,20H ;blank space
INT 21H
https://pucit-online.blogspot.com
91
;--- Chapter Eight
DEC counter2
CMP counter2,0 ;is there 4-number on line
JE NEW_LINE
DEC Counter
CMP Counter,0
JNE L10
JMP OUT_
;-------------------------
Main ENDP
;see Appendix C for include pseudo-ops
;Test is folder’s name in c:\ contain procedures
INCLUDE C:\TEST\RANDOM.ASM
INCLUDE C:\TEST\INBINARY.ASM
INCLUDE C:\TEST\OUTBIN.ASM
END Main
https://pucit-online.blogspot.com
92
Chapter Eight
https://pucit-online.blogspot.com
93
Chapter Nine
(1)
DX AX CF/OF
a) 0000 0018 0
b) 000F F000 1
c) FFFF FFFB 0
d) 0000 8000 1
e) Illegal : source must be register or M.L
(2)
AX CF/OF
a) 0AB0 1
b) FAB0 1
c) 00AB 0
d) FFF6 0
(3)
DX AX
a) 0001 0003
b) 000E 0FFF
c) FFFF FFFF
d) divide overflow
https://pucit-online.blogspot.com
94
Chapter Nine
(4)
AH AL
a) 01 04
b) FF 02
c) 0E 0F
d) divide overflow
(5)
DX
a) 0000
b) FFFF
c) 0000
(6)
AX
a) FFF0
b) 00F5
c) FF80
https://pucit-online.blogspot.com
95
Chapter Nine
(7)
a) MOV AX,5 ; AX=5
IMUL A ;AX=5*A
SUB AX,7 ;AX=5*A-7
MOV A,AX ;A=5*A-7
https://pucit-online.blogspot.com
96
Chapter Nine
END_IF:
https://pucit-online.blogspot.com
97
Chapter Nine
Programming Exercises
(8)
https://pucit-online.blogspot.com
98
Chapter Nine
(9)
.MODEL SMALL
.STACK 64
.DATA
M1 DB 0AH,0DH,'THE TIME IS $'
.CODE
MAIN PROC
MOV AX,@DATA ;intialize DS
MOV DS,AX
CALL INDEC
MOV DX,0 ;make sure no thing in DX
MOV BX,3600 ;60min*60sec=1hr
DIV BX
PUSH AX
PUSH DX
;---------
POP DX
POP AX ;AX has hours
CALL OUTDEC
PUSH DX
https://pucit-online.blogspot.com
99
Chapter Nine
MOV AH,2
MOV DL,':'
INT 21H
;----------
POP AX
MOV BX,60
MOV DX,0
DIV BX
CALL OUTDEC ;AX has mins
PUSH DX
;-----------
MOV AH,2
MOV DL,':'
INT 21H
POP AX
CALL OUTDEC ;AX has sec remainder
;----------
MOV AH,4CH ;return to DOS
INT 21H
MAIN ENDP
INCLUDE C:\TEST\OUTDEC.ASM
INCLUDE C:\TEST\INDEC.ASM
END MAIN
https://pucit-online.blogspot.com
100
Chapter Nine
https://pucit-online.blogspot.com
101
Chapter Nine
(10)
Note: Half = 50 cents
Quarter = 25 cents
Dime = 10 cents
Nickle = 5 cents
Penny = 1 cents
.MODEL SMALL
.DATA
START DB 0AH,0DH
HLF DB ?,' Half '
QUA DB ?,' Quarter '
DIM DB ?,' Dimes '
NIC DB ?,' Nickels '
PEN DB ?,' Pennies ','$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
;-------------------------------------------------
CALL INDEC ;AX has cents
https://pucit-online.blogspot.com
102
Chapter Nine
;----------------------
MOV AL,AH
MOV AH,0
MOV BL,25 ;Quarter
DIV BL
ADD AL,30h
MOV QUA,AL
;---------------------
MOV AL,AH
MOV AH,0
MOV BL,10 ;DIME
DIV BL
ADD AL,30H
MOV DIM,AL
;-------------------
MOV AL,AH
MOV AH,0
MOV BL,5 ;nickle
DIV BL
ADD AL,30h
MOV NIC,AL
;----------------
ADD AH,30h
MOV PEN,AH ;remainder is pennies
;---------------------------------------------------------
MOV AH,9 ;display result
LEA DX,START
INT 21H
https://pucit-online.blogspot.com
103
Chapter Nine
;-----------------
MOV AH,4CH
INT 21H
MAIN ENDP
INCLUDE C:\TEST\INDEC.ASM
END MAIN
https://pucit-online.blogspot.com
104
Chapter Nine
(11)
.MODEL SMALL
.DATA
M1 DB 0AH,0DH,'enter M : $'
M2 DB 0AH,0DH,'enter N : $'
M3 DB 0AH,0DH,'RESULT IS $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
MOV AH,9
LEA DX,M1
INT 21H
;--------
CALL INDEC ;enter M
MOV BX,AX ; M in BX
MOV AH,9
LEA DX,M2
INT 21H
CALL INDEC ; read N in AX
PUSH AX
;----------------
MOV AH,9
LEA DX,M3
INT 21H
MOV AH,2 ;print " . "
MOV DL,'.'
https://pucit-online.blogspot.com
105
Chapter Nine
INT 21H
;----------------
POP AX
XCHG AX,BX ;AX is M BX is N
CALL OUTDEC
;----------------
;------------------------------
MOV AH,4CH
INT 21H
MAIN ENDP
INCLUDE C:\TEST\INDEC.ASM
INCLUDE C:\TEST\OUTDEC.ASM
END MAIN
https://pucit-online.blogspot.com
106
Chapter Nine
https://pucit-online.blogspot.com
107
Chapter Nine
(18)
.MODEL SMALL
.DATA
M1 DB 0AH,0DH,'ENTER M $'
M2 DB 0AH,0DH,'ENTER N $'
M3 DB 0AH,0DH,'GCD IS $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
;-----------------
MOV AH,9
LEA DX,M1
INT 21H
CALL INDEC ;read M
PUSH AX ;save M
MOV AH,9
LEA DX,M2
INT 21H
CALL INDEC ;read N
PUSH AX ;save N
;-------------
POP BX
POP AX
L1: MOV DX,0
DIV BX ;M/N
CMP DX,0
JE GCD_FOUND ;BX IS N
https://pucit-online.blogspot.com
108
Chapter Nine
GCD_FOUND:
MOV AH,9
LEA DX,M3
INT 21H
MOV AX,BX
CALL OUTDEC
;-----------------
MOV AH,4CH
INT 21H
MAIN ENDP
INCLUDE C:\TEST\OUTDEC.ASM
INCLUDE C:\TEST\INDEC.ASM
END MAIN
https://pucit-online.blogspot.com
109
Chapter Nine
https://pucit-online.blogspot.com
110
Chapter Ten
(1)
a) legal DI=1500h
b) legal DI=0200h
c) legal AX=0650h
d) legal BX=0E00h
e) legal BX=2000h
f) illegal memory-to-memory
g) illegal []must be 16-bit
h) illegal different size
i) legal AX=0300h
(2)
a) legal AH=01h
b) legal AX=0504
c) legal AX=’BA’
d) illegal different size
e) legal AH=’A’
(3)
a)
MOV BP,SP
MOV [BP],0
MOV [BP+2],0
https://pucit-online.blogspot.com
111
Chapter Ten
b)
.DATA
ST_ARR DW 5 DUP(?)
.CODE
MOV BX,0
MOV CX,5
MOV BP,SP
L1: MOV AX,WORD PTR [BP]
MOV WORD PTR ST_ARR[BX] , AX
ADD BP,2
ADD BX,2
LOOP L1
https://pucit-online.blogspot.com
112
Appendix A
Step # 2:
- Use an assembler (MASM or TASM) to create a
machine language … (i.e : create the object file with
extension *.OBJ)
- You can do this by using the command (if we suppose
work on TASM) tasm followed by the name of the
source file with its extension… knoing that you are
already in path of file (ex: tasm page1.ASM)
- After doing this assembler do its work work and
display some info. Like copy right and most important
thig error messages and they are located.
- By check no errors you can continue else you must go
bace to source file and fix the error.
https://pucit-online.blogspot.com
113
Appendix A
Step # 3:
- use the link program to link one or more object files to
create a Run file with extension *.EXE
- you can do this by using command … tlink with name
of file , no need to include the extension here… (ex:
tlink page1)
Step # 4:
- here the executable file is created
- you can run it by typing only the name of file …(ex:
page1)
https://pucit-online.blogspot.com
114
Appendix A
Step # 1:
Write the code of program by using notepad editor
Save the file with name student.ASM in derive C: inside
folder its name test
(the file save in path c:\test\student.asm)
Step # 2 :
- Open command prompt (you can open it by typing
cmd in run window)
https://pucit-online.blogspot.com
115
Appendix A
- go back to drive c:\
by use the command cd\
Error msg
No error
https://pucit-online.blogspot.com
116
Appendix A
Step # 3:
Use tlink command
Step # 4:
Now you can run the program by typing only its name or
with the extesion *.exe
After this if you open the folder test in c: drive you will see
4-files ,
Student.ASM source file
Student.OBJ object file produced by tasm
Student.MAP contain segment addresses
Student.EXE run program (you can run it by direct
click on it )
https://pucit-online.blogspot.com
117
Appendix B
Input in Decimal
INDEC PROC
;READ NUMBER IN RANGE -32768 TO 32767
;input :none
;output :AX =binary equivalent of number
PUSH BX
PUSH CX
PUSH DX
;print prompt
@BEGIN:
MOV AH,2
MOV DL,'?'
INT 21H ;print '?'
;total =0
XOR BX,BX ;BX hold total
;negative =false
XOR CX,CX ;CX hold sign
;read char.
MOV AH,1
INT 21H
;case char. of
CMP AL,'-' ;minus sign
JE @MINUS ;yes,set sign
CMP AL,'+' ;plus sign
JE @PLUS ;yes,get another char.
JMP @REPEAT2 ;start processing char.
https://pucit-online.blogspot.com
118
Appendix B
https://pucit-online.blogspot.com
119
Appendix B
POP BX
RET
;here if illegal char entered
@NOT_DIGIT:
MOV AH,2
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
JMP @BEGIN
INDEC ENDP
https://pucit-online.blogspot.com
120
Appendix B
Input in Decimal
With overflow check
INDEC2 PROC
;READ NUMBER IN RANGE -32768 TO 32767
;input :none
;output :AX =binary equivalent of number
PUSH BX
PUSH CX
PUSH DX
;print prompt
@BEGIN:
MOV AH,2
MOV DL,'?'
INT 21H ;print '?'
;total =0
XOR BX,BX ;BX hold total
;negative =false
XOR CX,CX ;CX hold sign
;read char.
MOV AH,1
INT 21H
;case char. of
CMP AL,'-' ;minus sign
JE @MINUS ;yes,set sign
CMP AL,'+' ;plus sign
JE @PLUS ;yes,get another char.
JMP @REPEAT2 ;start processing char.
https://pucit-online.blogspot.com
121
Appendix B
https://pucit-online.blogspot.com
122
Appendix B
JE @EXIT
NEG AX
;end if
@EXIT: POP DX
POP CX
POP BX
RET
;here if illegal char entered
@NOT_DIGIT:
MOV AH,2
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
JMP @BEGIN
INDEC2 ENDP
https://pucit-online.blogspot.com
123
Appendix B
Output in Decimal
OUTDEC PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
OR AX,AX
JGE @END_IF1
PUSH AX
MOV DL,'-'
MOV AH,2
INT 21H
POP AX
NEG AX
@END_IF1:
XOR CX,CX
MOV BX,10D
@REPEAT1:
XOR DX,DX
DIV BX
PUSH DX
INC CX
https://pucit-online.blogspot.com
124
Appendix B
OR AX,AX
JNE @REPEAT1
MOV AH,2
@PRINT_LOOP:
POP DX
OR DL,30H
INT 21H
LOOP @PRINT_LOOP
POP DX
POP CX
POP BX
POP AX
RET
OUTDEC ENDP
https://pucit-online.blogspot.com
125
Appendix B
Input in Binary
INBINARY PROC
XOR BX,BX
MOV AH,1
INT 21H
WHILE_:
CMP AL,0DH
JE END_WHILE
AND AL,0FH
SHL BX,1
OR BL,AL
INT 21H
JMP WHILE_
END_WHILE:
RET
INBINARY ENDP
https://pucit-online.blogspot.com
126
Appendix B
Output in Binary
OUTBINARY PROC
;input AX
MOV BX,AX
MOV CX,16
MOV AH,2
MOV DL,'0'
INT 21H
JMP L1
ONE:
MOV AH,2
MOV DL,'1'
INT 21H
L1: LOOP L0
RET
OUTBINARY ENDP
https://pucit-online.blogspot.com
127
Appendix B
Input Sting
https://pucit-online.blogspot.com
128
Appendix B
Output Sting
https://pucit-online.blogspot.com
129
Wriiten by:
Engineer Amer Mohammed Al-khsabah
Eng.Amer.K@hotmail.com
https://pucit-online.blogspot.com