SlideShare a Scribd company logo
Microprocessor Based Systems
Spring 2013
Department of Electrical Engineering
University of Gujrat
2
IBM
CHARACTER
DISPLAY
TITLE IBM CHARACTER DISPLAY
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOVAH, 2 ; display char function
MOVCX, 256 ; no. of chars to display
MOVDL, 0 ; DL has ASCII code of null char
PRINT_LOOP:
INT 21h ; display a char
INC DL ; increment ASCII code
DEC CX ; decrement counter
JNZ PRINT_LOOP ; keep going if CX not 0
; DOS exit
MOVAH, 4CH
INT 21H
MAIN ENDP
END MAIN
3
Label
4
5
IBM Character Display
• IBM.ASM
6
Conditional Jumps
• Jxxx destination_label
• In IBM.ASM, the CPU executes JNZ
PRINT_LOOP by inspecting ZF.
• If ZF = 0, control transfers to PRINT_LOOP
• If ZF = 1, it goes on to execute MOV AH, 4CH
• Jump instructions themselves do not affect
the flags.
7
Range of a conditional jump
• destination_label must precede the jump
instruction by no more than 126 bytes, or
follow it by no more than 127 bytes.
8
jumps
Conditional
jumps
Signed Jumps
Unsigned
Jumps
Single Flag
Jumps
Unconditional
jumps
9
The CMP (compare) Instruction
• CMP destination, source
• CMP is just like SUB, except that destination is
not changed.
• CMP AX, BX ; AX = 7FFFh, BX = 0001h
JG BELOW ; AX – BX = 7FFEh
• The jump condition for JG is satisfied because
ZF = SF = OF = 0, so control transfers to label
BELOW.
10
Interpreting the Conditional Jumps
• CMP AX, BX
JG BELOW
• If AX is greater than BX (in a signed sense), then
JG (jump if greater than) transfers to BELOW.
• DEC AX
JL THERE
• If the contents of AX, in a signed sense, is less
than 0, control transfers to THERE.
11
12
Jumps Based on Specific Flags
13
Jumps Based on Unsigned
Comparisons
14
Jumps Based on Signed Comparisons
Signed Versus Unsigned Jumps
• CMP AX, BX ; AX = 7FFFh, BX = 8000h
JA BELOW
• 7FFFh > 8000h in a signed sense, the program
does not jump to BELOW.
• 7FFFh < 8000h in an unsigned sense, and we
are using the unsigned jump JA.
15
Suppose AX and BX contain signed numbers.
Write some code to put the biggest one in CX.
MOV CX, AX ; put AX in CX
CMP BX, CX ; is BX bigger?
JLE NEXT ; no, go on
MOV CX, BX ; yes, put BX in CX
NEXT:
16
The JMP Instruction
• JMP destination
• JMP can be used to get around the range
restriction of a conditional jump.
17
Unconditional Jump
TOP:
; body of the loop
DEC CX ; decrement counter
JNZ TOP ; keep looping if CX > 0
MOV AX, BX
; the loop body contains so many instructions
that label TOP is out of range for JNZ
(more than 126 bytes before JMP TOP)
18
Unconditional Jump
TOP:
; body of the loop
DEC CX ; decrement counter
JNZ BOTTOM ; keep looping if CX > 0
JMP EXIT
BOTTOM:
JMP TOP
EXIT:
MOV AX, BX
19
High level language constructs
20
IF-THEN
IF condition is true
THEN
execute true-branch statements
END_IF
21
Replace the number in AX by its
absolute value.
IF AX < 0
THEN
replace AX by –AX
END_IF
22
Replace the number in AX by its
absolute value.
; if AX < 0
CMP AX, 0 ; AX < 0 ?
JNL END_IF ; no, exit
; then
NEG AX ; yes, change sign
END IF:
23
IF-THEN-ELSE
IF condition is true
THEN
execute true-branch statements
ELSE
execute false-branch statements
END_IF
24
Suppose AL and BL contain extended ASCII
characters. Display the one that comes first
in the character sequence.
IF AL <= BL
THEN
display the character in AL
ELSE
display the character in BL
END_IF
25
Suppose AL and BL contain extended ASCII
characters. Display the one that comes first
in the character sequence.
MOV AH, 2 ; prepare to display
; if AL <= BL
CMP AL, BL ; AL <= BL?
JNBE ELSE_ ; no, display char in BL
; then ; AL <= BL
MOV DL, AL ; move char to be displayed
JMP DISPLAY ; go to display
ELSE_: ; BL < AL
MOV DL, BL
DISPLAY:
INT 21h ; display it
END_IF
26
ELSE is a
reserved word
Needed to skip false
branch (not needed in
high level language)
CASE
CASE expression
value 1 : statements_1
value 2 : statements_2
.
.
.
value n : statements_n
END_CASE
27
If AX contains a negative number, put –1 in
BX; if AX contains 0, put 0 in BX, if AX
contains a positive number , put 1 in BX.
CASE AX
<0 : put –1 in BX
=0 : put 0 in BX
>0 : put 1 in BX
END_CASE
28
If AX contains a negative number, put –1 in
BX; if AX contains 0, put 0 in BX, if AX
contains a positive number , put 1 in BX.
; case AX
CMP AX, 0 ; test AX
JL NEGATIVE ; AX < 0
JE ZERO ; AX = 0
JG POSITIVE ; AX > 0
NEGATIVE:
MOV BX, -1 ; put -1 in BX
JMP END_CASE ; and exit
ZERO:
MOV BX, 0 ; put -0in BX
JMP END_CASE ; and exit
POSITIVE:
MOV BX, 1 ; put 1 in BX
END_CASE:
29
Only one cmp is
needed as jump
instructions don’t
affect the flags
If AL contains 1 or 3, display “o”;
If AL contains 2 or 4, display “e”.
CASE AL
1, 3 : display “o”
2, 4 : display “e”
END_CASE
30
If AL contains 1 or 3, display “o”;
If AL contains 2 or 4, display “e”.
; case AL
; 1,3 :
CMP AL, 1 ; AL = 1?
JE ODD ; yes, display ‘o’
CMP AL ,3 ; AL = 3?
JE ODD ; yes, display ‘o’
; 2,4 :
CMP AL, 2 ; AL = 2?
JE EVEN ; yes, display ‘e’
CMP AL, 4 ; AL = 4?
JE EVEN ; yes, display ‘e’
JMP END_CASE ; not 1..4
31
If AL contains 1 or 3, display “o”;
If AL contains 2 or 4, display “e”.
ODD: ; display ‘o’
MOV DL, ‘o’ ; get ‘o’
JMP DISPLAY ; go to display
EVEN: ; display ‘e’
MOV DL, ‘e’ ; get ‘e’
DISPLAY:
MOV AH, 2
INT 21H ; display char
END_CASE:
32
Branches with Compound Conditions
• Some times the branching in an IF or CASE
takes from;
• condition_1 AND condition_2
or
condition_1 OR condition_2
33
AND Conditions
• condition_1 AND condition_2
• An AND condition is true if and only if
condition_1 and condition_2 are both true.
• If either condition is false, then the whole
thing is false.
34
Read a character, and if it’s an
uppercase letter, display it.
Read a character (into AL)
IF (‘A’ <= character) and (character <= ‘Z’)
THEN
display character
END_IF
35
Read a character, and if it’s an
uppercase letter, display it.
; read a character
MOV AH, 1 ; prepare to read
INT 21H ; char in AL
; if (‘A’ <= char) and (char >= ‘Z’)
CMP AL, ‘A’ ; char >= ‘A’?
JNGE END_IF ; no, exit
CMP AL, ‘Z’ ; char <= ‘Z’?
JNLE END_IF ; no, exit
; then display char
MOV DL, AL ; get char
MOV AH, 2 ; prepare to display
INT 21H ; display char
END_IF:
36
OR Conditions
• condition_1 OR condition_2
• condition_1 OR condition_2 is true if at least
one of the conditions is true.
• It is only false when both conditions are false.
37
Read a character, and if it is “y” or “Y”, display
it; otherwise, terminate the program.
Read a character (into AL)
IF (character = ‘y’) or (character = ‘Y’)
THEN
display it
ELSE
terminate the program
END_IF
38
Read a character, and if it is “y” or “Y”, display
it; otherwise, terminate the program.
; read a character
MOV AH, 1 ; prepare to read
INT 21H ; char in AL
; if (character = ‘y’) or (character = ‘Y’)
CMP AL, ‘y’ ; char = ‘y’?
JE THEN ; yes, go to display it
CMP AL, ‘Y’ ; char = ‘Y’?
JE THEN ; yes, go to display it
JMP ELSE_ ; no, terminate
39
Read a character, and if it is “y” or “Y”, display
it; otherwise, terminate the program.
THEN:
MOV AH, 2 ; prepare to display
MOV DL, AL ; get char
INT 21H ; display it
JMP END_IF ; end exit
ELSE_:
MOV AH, 4CH
INT 21H ; DOS exit
END_IF:
40
Looping Structures
• A loop is a sequence of instructions that is
repeated .
• The number of times to repeat may be known
in advance
or
• Depend on some condition
41
FOR LOOP
Loop statements are repeated a known number
of times;
FOR loop_count times DO
statements
END_FOR
42
The LOOP instruction
• LOOP destination_label
; initialize CX to loop_count
TOP:
; body of the loop
LOOP TOP
43
The LOOP instruction
• The counter for the loop is the register CX which
is initialized to loop_count.
• Execution of the LOOP instruction causes CX to be
decremented automatically.
• If CX is not 0, control transfers to
destination_label.
• If CX = 0, the next instruction after LOOP is done.
• destination_label must precede the LOOP
instruction by no more than 126 bytes.
44
Write a count-controlled loop to
display a row of 80 stars.
FOR 80 times DO
display ‘*’
END_FOR
45
Write a count-controlled loop to
display a row of 80 stars.
MOV CX, 80 ; number of stars to display
MOV AH, 2 ; display character function
MOV DL, ‘*’ ; character to display
TOP:
INT 21h ; display a star
LOOP TOP ; repeat 80 times
46
The instruction JCXZ (jump if CX is zero)
• JCXZ destination_label
JCXZ SKIP
TOP:
; body of the loop
LOOP TOP
SKIP:
47
The instruction JCXZ (jump if CX is zero)
• If CX contains 0 when the loop is entered, the
LOOP instruction causes CX to be
decremented to FFFFh, and the loop is then
executed FFFFh = 65535 more times!
48
WHILE condition DO
statements
END_WHILE
_______________________________________
REPEAT
statements
UNTIL condition
WHILE LOOP and REPEAT LOOP
49
Statements
Condition
Statements
Condition
Write some code to count the number
of characters in an input line.
Initialize count to 0
read a character
WHILE character <> carriage_return DO
count = count + 1
read a character
END_WHILE
50
Write some code to count the number
of characters in an input line.
MOV DX, 0 ; DX counts characters
MOV AH, 1 ; prepare to read
INT 21H ; character in AL
WHILE_:
CMP AL, 0DH ; CR?
JE END_WHILE ; yes, exit
INC DX ; not CR, increment count
INT 21H ; read a character
JMP WHILE_ ; loop back
END_WHILE_:
51
Write some code to read characters
until a blank is read.
REPEAT
read a character
UNTIL character is a blank
______________________________________________
MOV AH, 1 ; prepare to read
REPEAT:
INT 21H ; char in AL
; until
CMP AL, ‘ ‘ ; a blank?
JNE REPEAT ; no, keep reading
52
Programming with High-Level Structures
• CAP.ASM
Type a line of text:
THE QUICK BROWN FOX JUMPED.
First capital = B Last capital = X
53
If no capital letter entered,
display
“No capital letter entered”
Read and process a line of text
Read a character
WHILE character is not a carriage return DO
IF character is a capital letter (‘A’ <= character AND character <= ‘Z’)
THEN
IF character precedes first capital
THEN first capital = character
END IF
IF character follows last capital
THEN last capital = character
END IF
END IF
Read a character
END_WHILE
54
Display the results
IF no capitals were typed,
THEN
display “No capitals”
ELSE
display first capital and last capital
END_IF
55
ASCII
Character
Table
FIRST
LAST
CAP.ASM
1(4)
TITLE FIRST AND LAST CAPITALS
.MODEL SMALL
.STACK 100H
.DATA
PROMPT DB 'Type a line of text', 0DH,
0AH, '$'
NOCAP_MSG DB 0DH, 0AH, 'No capitals $'
CAP_MSG DB 0DH, 0AH, 'First capital =
'
FIRST DB '['
DB ' Last capital = '
LAST DB '@ $'
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
; display opening message
MOV AH, 9 ; display string function
LEA DX, PROMPT ; get opening message
INT 21H ; display it
; read and process a line of text
MOV AH, 1 ; read char function
INT 21H ; char in AL
WHILE_:
; while character is not a carriage return do
CMP AL, 0DH ; CR?
JE END_WHILE ; yes, exit
; if character is a capital letter
CMP AL, 'A' ; char >= 'A'?
JNGE END_IF ; not a capital letter
CMP AL, 'Z' ; chat <= 'Z'?
JNLE END_IF ; not a capital letter
; then
CAP.ASM
2(4)
CAP.ASM
3(4)
; if character precedes first capital
CMP AL, FIRST ; char < first capital?
JNL CHECK_LAST ; no, >=
; then first capital = character
MOV FIRST, AL ; FIRST = char
; end_if
CHECK_LAST:
; if character follows last capital
CMP AL, LAST ; char > last capital?
JNG END_IF ; no, <=
; then last capital = character
MOV LAST, AL ; LAST = char
; end_if
END_IF:
; read a character
INT 21H ; char in AL
JMP WHILE_ ; repeat loop
END_WHILE:
; display results
MOVAH, 9 ; display string function
; if no capitals were typed
CMPFIRST, '['; first = '['
JNECAPS ; no, display results
; then
LEADX, NOCAP_MSG ; no capitals
JMPDISPLAY
CAPS:
LEADX, CAP_MSG ; capitals
DISPLAY:
INT21H ; display message
; end_if
; dos exit
MOVAH, 4CH
INT21H
MAIN ENDP
END MAIN
CAP.ASM
4(4)

More Related Content

PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 10 ( Arrays and ...
PDF
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
PPT
Assembly language programming_fundamentals 8086
PPT
Assembly Language Lecture 5
PDF
chapter 7 Logic, shift and rotate instructions
PDF
Introduction to ibm pc assembly language
PPTX
Flow control instructions
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 10 ( Arrays and ...
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Assembly language programming_fundamentals 8086
Assembly Language Lecture 5
chapter 7 Logic, shift and rotate instructions
Introduction to ibm pc assembly language
Flow control instructions

What's hot (20)

PDF
Chapter 5The proessor status and the FLAGS registers
PPT
Assembly Language Lecture 4
PPTX
PPTX
8085 microprocessor
PPTX
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
PDF
Chapter 6 Flow control Instructions
PPT
Ll(1) Parser in Compilers
PPTX
Microprocessor 8086 instruction description
PPTX
Logical and shift micro operations
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 5 (The Processor...
PPTX
Logic simplification sop and pos forms
PPTX
Threaded Binary Tree
PPTX
Instruction sets of 8086
PPTX
Ascii arithmetic instructions
PPT
RECURSION IN C
PPT
Jumps in Assembly Language.
PPTX
Decoders-Digital Electronics
PPT
Function overloading(c++)
PPTX
AVL Tree in Data Structure
Chapter 5The proessor status and the FLAGS registers
Assembly Language Lecture 4
8085 microprocessor
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Chapter 6 Flow control Instructions
Ll(1) Parser in Compilers
Microprocessor 8086 instruction description
Logical and shift micro operations
Assembly Language Programming By Ytha Yu, Charles Marut Chap 5 (The Processor...
Logic simplification sop and pos forms
Threaded Binary Tree
Instruction sets of 8086
Ascii arithmetic instructions
RECURSION IN C
Jumps in Assembly Language.
Decoders-Digital Electronics
Function overloading(c++)
AVL Tree in Data Structure
Ad

Viewers also liked (20)

PPT
Chapter 1
PDF
assembly language programming and organization of IBM PC" by YTHA YU
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 1(Microcomputer ...
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
PPTX
Processor Basics
PPT
Assembly Language Lecture 2
PPT
Assembly Language Basics
PPT
Assembly language programming(unit 4)
PDF
Chapter 3
PDF
Chapter 4
PPT
Assignment on alp
PPTX
8086 microprocessor-architecture-120207111857-phpapp01
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
PDF
Matlab 1
PDF
Matlab 2
PDF
Matlab 3
PDF
Coal 2 - concepts in Assembly Programming
PDF
Coal 1 - introduction to assembly programming in Assembly Programming
PPTX
bubble sorting of an array in 8086 assembly language
PPTX
Conditional jump
Chapter 1
assembly language programming and organization of IBM PC" by YTHA YU
Assembly Language Programming By Ytha Yu, Charles Marut Chap 1(Microcomputer ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Processor Basics
Assembly Language Lecture 2
Assembly Language Basics
Assembly language programming(unit 4)
Chapter 3
Chapter 4
Assignment on alp
8086 microprocessor-architecture-120207111857-phpapp01
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Matlab 1
Matlab 2
Matlab 3
Coal 2 - concepts in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly Programming
bubble sorting of an array in 8086 assembly language
Conditional jump
Ad

Similar to Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control Instructions,) (20)

PPTX
lecture4 control flow instruction in assembly.pptx
PPT
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
PPTX
Loop instruction, controlling the flow of progam
PPT
Chapt 06
PPT
Chapt 06
PPTX
[ASM]Lab5
PPT
Al2ed chapter7
PDF
Intel codetable
PPT
1344 Alp Of 8086
PPTX
Flag control
PDF
Taller practico emu8086_galarraga
PPTX
[ASM]Lab7
PPTX
Assembly language programs
PDF
Taller Ensambladores
PDF
Assembly Complete 8086 Instruction Set
PPTX
L12_ COA_COmputer architecurte and organization
PPTX
Assembly lab up to 6 up (1)
PPT
Assignment on alp
PDF
Assembly language 8086
DOCX
Assembly language
lecture4 control flow instruction in assembly.pptx
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
Loop instruction, controlling the flow of progam
Chapt 06
Chapt 06
[ASM]Lab5
Al2ed chapter7
Intel codetable
1344 Alp Of 8086
Flag control
Taller practico emu8086_galarraga
[ASM]Lab7
Assembly language programs
Taller Ensambladores
Assembly Complete 8086 Instruction Set
L12_ COA_COmputer architecurte and organization
Assembly lab up to 6 up (1)
Assignment on alp
Assembly language 8086
Assembly language

More from Bilal Amjad (12)

PDF
IoT Based Smart Energy Meter using Raspberry Pi and Arduino
PDF
Power Systems analysis with MATPOWER and Simscape Electrical (MATLAB/Simulink)
PDF
Solar Radiation monthly prediction and forecasting using Machine Learning tec...
PDF
Big Data in Smart Grid
PPTX
Flexibility of Power System (Sources of flexibility & flexibility markets)
PPTX
Binary and hex input/output (in 8086 assembuly langyage)
PPTX
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
PPTX
assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...
PPTX
Limit of complex number
PPTX
simple combinational lock
PPTX
4-bit camparator
PPTX
Orthogonal trajectories
IoT Based Smart Energy Meter using Raspberry Pi and Arduino
Power Systems analysis with MATPOWER and Simscape Electrical (MATLAB/Simulink)
Solar Radiation monthly prediction and forecasting using Machine Learning tec...
Big Data in Smart Grid
Flexibility of Power System (Sources of flexibility & flexibility markets)
Binary and hex input/output (in 8086 assembuly langyage)
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...
Limit of complex number
simple combinational lock
4-bit camparator
Orthogonal trajectories

Recently uploaded (20)

PDF
composite construction of structures.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
composite construction of structures.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CH1 Production IntroductoryConcepts.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Lecture Notes Electrical Wiring System Components
573137875-Attendance-Management-System-original
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Foundation to blockchain - A guide to Blockchain Tech
Embodied AI: Ushering in the Next Era of Intelligent Systems
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
Sustainable Sites - Green Building Construction

Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control Instructions,)

  • 1. Microprocessor Based Systems Spring 2013 Department of Electrical Engineering University of Gujrat
  • 2. 2
  • 3. IBM CHARACTER DISPLAY TITLE IBM CHARACTER DISPLAY .MODEL SMALL .STACK 100H .CODE MAIN PROC MOVAH, 2 ; display char function MOVCX, 256 ; no. of chars to display MOVDL, 0 ; DL has ASCII code of null char PRINT_LOOP: INT 21h ; display a char INC DL ; increment ASCII code DEC CX ; decrement counter JNZ PRINT_LOOP ; keep going if CX not 0 ; DOS exit MOVAH, 4CH INT 21H MAIN ENDP END MAIN 3 Label
  • 4. 4
  • 5. 5
  • 7. Conditional Jumps • Jxxx destination_label • In IBM.ASM, the CPU executes JNZ PRINT_LOOP by inspecting ZF. • If ZF = 0, control transfers to PRINT_LOOP • If ZF = 1, it goes on to execute MOV AH, 4CH • Jump instructions themselves do not affect the flags. 7
  • 8. Range of a conditional jump • destination_label must precede the jump instruction by no more than 126 bytes, or follow it by no more than 127 bytes. 8
  • 10. The CMP (compare) Instruction • CMP destination, source • CMP is just like SUB, except that destination is not changed. • CMP AX, BX ; AX = 7FFFh, BX = 0001h JG BELOW ; AX – BX = 7FFEh • The jump condition for JG is satisfied because ZF = SF = OF = 0, so control transfers to label BELOW. 10
  • 11. Interpreting the Conditional Jumps • CMP AX, BX JG BELOW • If AX is greater than BX (in a signed sense), then JG (jump if greater than) transfers to BELOW. • DEC AX JL THERE • If the contents of AX, in a signed sense, is less than 0, control transfers to THERE. 11
  • 12. 12 Jumps Based on Specific Flags
  • 13. 13 Jumps Based on Unsigned Comparisons
  • 14. 14 Jumps Based on Signed Comparisons
  • 15. Signed Versus Unsigned Jumps • CMP AX, BX ; AX = 7FFFh, BX = 8000h JA BELOW • 7FFFh > 8000h in a signed sense, the program does not jump to BELOW. • 7FFFh < 8000h in an unsigned sense, and we are using the unsigned jump JA. 15
  • 16. Suppose AX and BX contain signed numbers. Write some code to put the biggest one in CX. MOV CX, AX ; put AX in CX CMP BX, CX ; is BX bigger? JLE NEXT ; no, go on MOV CX, BX ; yes, put BX in CX NEXT: 16
  • 17. The JMP Instruction • JMP destination • JMP can be used to get around the range restriction of a conditional jump. 17
  • 18. Unconditional Jump TOP: ; body of the loop DEC CX ; decrement counter JNZ TOP ; keep looping if CX > 0 MOV AX, BX ; the loop body contains so many instructions that label TOP is out of range for JNZ (more than 126 bytes before JMP TOP) 18
  • 19. Unconditional Jump TOP: ; body of the loop DEC CX ; decrement counter JNZ BOTTOM ; keep looping if CX > 0 JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX 19
  • 20. High level language constructs 20
  • 21. IF-THEN IF condition is true THEN execute true-branch statements END_IF 21
  • 22. Replace the number in AX by its absolute value. IF AX < 0 THEN replace AX by –AX END_IF 22
  • 23. Replace the number in AX by its absolute value. ; if AX < 0 CMP AX, 0 ; AX < 0 ? JNL END_IF ; no, exit ; then NEG AX ; yes, change sign END IF: 23
  • 24. IF-THEN-ELSE IF condition is true THEN execute true-branch statements ELSE execute false-branch statements END_IF 24
  • 25. Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence. IF AL <= BL THEN display the character in AL ELSE display the character in BL END_IF 25
  • 26. Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence. MOV AH, 2 ; prepare to display ; if AL <= BL CMP AL, BL ; AL <= BL? JNBE ELSE_ ; no, display char in BL ; then ; AL <= BL MOV DL, AL ; move char to be displayed JMP DISPLAY ; go to display ELSE_: ; BL < AL MOV DL, BL DISPLAY: INT 21h ; display it END_IF 26 ELSE is a reserved word Needed to skip false branch (not needed in high level language)
  • 27. CASE CASE expression value 1 : statements_1 value 2 : statements_2 . . . value n : statements_n END_CASE 27
  • 28. If AX contains a negative number, put –1 in BX; if AX contains 0, put 0 in BX, if AX contains a positive number , put 1 in BX. CASE AX <0 : put –1 in BX =0 : put 0 in BX >0 : put 1 in BX END_CASE 28
  • 29. If AX contains a negative number, put –1 in BX; if AX contains 0, put 0 in BX, if AX contains a positive number , put 1 in BX. ; case AX CMP AX, 0 ; test AX JL NEGATIVE ; AX < 0 JE ZERO ; AX = 0 JG POSITIVE ; AX > 0 NEGATIVE: MOV BX, -1 ; put -1 in BX JMP END_CASE ; and exit ZERO: MOV BX, 0 ; put -0in BX JMP END_CASE ; and exit POSITIVE: MOV BX, 1 ; put 1 in BX END_CASE: 29 Only one cmp is needed as jump instructions don’t affect the flags
  • 30. If AL contains 1 or 3, display “o”; If AL contains 2 or 4, display “e”. CASE AL 1, 3 : display “o” 2, 4 : display “e” END_CASE 30
  • 31. If AL contains 1 or 3, display “o”; If AL contains 2 or 4, display “e”. ; case AL ; 1,3 : CMP AL, 1 ; AL = 1? JE ODD ; yes, display ‘o’ CMP AL ,3 ; AL = 3? JE ODD ; yes, display ‘o’ ; 2,4 : CMP AL, 2 ; AL = 2? JE EVEN ; yes, display ‘e’ CMP AL, 4 ; AL = 4? JE EVEN ; yes, display ‘e’ JMP END_CASE ; not 1..4 31
  • 32. If AL contains 1 or 3, display “o”; If AL contains 2 or 4, display “e”. ODD: ; display ‘o’ MOV DL, ‘o’ ; get ‘o’ JMP DISPLAY ; go to display EVEN: ; display ‘e’ MOV DL, ‘e’ ; get ‘e’ DISPLAY: MOV AH, 2 INT 21H ; display char END_CASE: 32
  • 33. Branches with Compound Conditions • Some times the branching in an IF or CASE takes from; • condition_1 AND condition_2 or condition_1 OR condition_2 33
  • 34. AND Conditions • condition_1 AND condition_2 • An AND condition is true if and only if condition_1 and condition_2 are both true. • If either condition is false, then the whole thing is false. 34
  • 35. Read a character, and if it’s an uppercase letter, display it. Read a character (into AL) IF (‘A’ <= character) and (character <= ‘Z’) THEN display character END_IF 35
  • 36. Read a character, and if it’s an uppercase letter, display it. ; read a character MOV AH, 1 ; prepare to read INT 21H ; char in AL ; if (‘A’ <= char) and (char >= ‘Z’) CMP AL, ‘A’ ; char >= ‘A’? JNGE END_IF ; no, exit CMP AL, ‘Z’ ; char <= ‘Z’? JNLE END_IF ; no, exit ; then display char MOV DL, AL ; get char MOV AH, 2 ; prepare to display INT 21H ; display char END_IF: 36
  • 37. OR Conditions • condition_1 OR condition_2 • condition_1 OR condition_2 is true if at least one of the conditions is true. • It is only false when both conditions are false. 37
  • 38. Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. Read a character (into AL) IF (character = ‘y’) or (character = ‘Y’) THEN display it ELSE terminate the program END_IF 38
  • 39. Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. ; read a character MOV AH, 1 ; prepare to read INT 21H ; char in AL ; if (character = ‘y’) or (character = ‘Y’) CMP AL, ‘y’ ; char = ‘y’? JE THEN ; yes, go to display it CMP AL, ‘Y’ ; char = ‘Y’? JE THEN ; yes, go to display it JMP ELSE_ ; no, terminate 39
  • 40. Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. THEN: MOV AH, 2 ; prepare to display MOV DL, AL ; get char INT 21H ; display it JMP END_IF ; end exit ELSE_: MOV AH, 4CH INT 21H ; DOS exit END_IF: 40
  • 41. Looping Structures • A loop is a sequence of instructions that is repeated . • The number of times to repeat may be known in advance or • Depend on some condition 41
  • 42. FOR LOOP Loop statements are repeated a known number of times; FOR loop_count times DO statements END_FOR 42
  • 43. The LOOP instruction • LOOP destination_label ; initialize CX to loop_count TOP: ; body of the loop LOOP TOP 43
  • 44. The LOOP instruction • The counter for the loop is the register CX which is initialized to loop_count. • Execution of the LOOP instruction causes CX to be decremented automatically. • If CX is not 0, control transfers to destination_label. • If CX = 0, the next instruction after LOOP is done. • destination_label must precede the LOOP instruction by no more than 126 bytes. 44
  • 45. Write a count-controlled loop to display a row of 80 stars. FOR 80 times DO display ‘*’ END_FOR 45
  • 46. Write a count-controlled loop to display a row of 80 stars. MOV CX, 80 ; number of stars to display MOV AH, 2 ; display character function MOV DL, ‘*’ ; character to display TOP: INT 21h ; display a star LOOP TOP ; repeat 80 times 46
  • 47. The instruction JCXZ (jump if CX is zero) • JCXZ destination_label JCXZ SKIP TOP: ; body of the loop LOOP TOP SKIP: 47
  • 48. The instruction JCXZ (jump if CX is zero) • If CX contains 0 when the loop is entered, the LOOP instruction causes CX to be decremented to FFFFh, and the loop is then executed FFFFh = 65535 more times! 48
  • 49. WHILE condition DO statements END_WHILE _______________________________________ REPEAT statements UNTIL condition WHILE LOOP and REPEAT LOOP 49 Statements Condition Statements Condition
  • 50. Write some code to count the number of characters in an input line. Initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 read a character END_WHILE 50
  • 51. Write some code to count the number of characters in an input line. MOV DX, 0 ; DX counts characters MOV AH, 1 ; prepare to read INT 21H ; character in AL WHILE_: CMP AL, 0DH ; CR? JE END_WHILE ; yes, exit INC DX ; not CR, increment count INT 21H ; read a character JMP WHILE_ ; loop back END_WHILE_: 51
  • 52. Write some code to read characters until a blank is read. REPEAT read a character UNTIL character is a blank ______________________________________________ MOV AH, 1 ; prepare to read REPEAT: INT 21H ; char in AL ; until CMP AL, ‘ ‘ ; a blank? JNE REPEAT ; no, keep reading 52
  • 53. Programming with High-Level Structures • CAP.ASM Type a line of text: THE QUICK BROWN FOX JUMPED. First capital = B Last capital = X 53 If no capital letter entered, display “No capital letter entered”
  • 54. Read and process a line of text Read a character WHILE character is not a carriage return DO IF character is a capital letter (‘A’ <= character AND character <= ‘Z’) THEN IF character precedes first capital THEN first capital = character END IF IF character follows last capital THEN last capital = character END IF END IF Read a character END_WHILE 54
  • 55. Display the results IF no capitals were typed, THEN display “No capitals” ELSE display first capital and last capital END_IF 55
  • 57. CAP.ASM 1(4) TITLE FIRST AND LAST CAPITALS .MODEL SMALL .STACK 100H .DATA PROMPT DB 'Type a line of text', 0DH, 0AH, '$' NOCAP_MSG DB 0DH, 0AH, 'No capitals $' CAP_MSG DB 0DH, 0AH, 'First capital = ' FIRST DB '[' DB ' Last capital = ' LAST DB '@ $' .CODE MAIN PROC ; initialize DS MOV AX, @DATA MOV DS, AX
  • 58. ; display opening message MOV AH, 9 ; display string function LEA DX, PROMPT ; get opening message INT 21H ; display it ; read and process a line of text MOV AH, 1 ; read char function INT 21H ; char in AL WHILE_: ; while character is not a carriage return do CMP AL, 0DH ; CR? JE END_WHILE ; yes, exit ; if character is a capital letter CMP AL, 'A' ; char >= 'A'? JNGE END_IF ; not a capital letter CMP AL, 'Z' ; chat <= 'Z'? JNLE END_IF ; not a capital letter ; then CAP.ASM 2(4)
  • 59. CAP.ASM 3(4) ; if character precedes first capital CMP AL, FIRST ; char < first capital? JNL CHECK_LAST ; no, >= ; then first capital = character MOV FIRST, AL ; FIRST = char ; end_if CHECK_LAST: ; if character follows last capital CMP AL, LAST ; char > last capital? JNG END_IF ; no, <= ; then last capital = character MOV LAST, AL ; LAST = char ; end_if END_IF: ; read a character INT 21H ; char in AL JMP WHILE_ ; repeat loop END_WHILE:
  • 60. ; display results MOVAH, 9 ; display string function ; if no capitals were typed CMPFIRST, '['; first = '[' JNECAPS ; no, display results ; then LEADX, NOCAP_MSG ; no capitals JMPDISPLAY CAPS: LEADX, CAP_MSG ; capitals DISPLAY: INT21H ; display message ; end_if ; dos exit MOVAH, 4CH INT21H MAIN ENDP END MAIN CAP.ASM 4(4)