lecture 4 presented by dr. shazzad hosain asst. prof. eecs, nsu

19
Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Upload: clement-shields

Post on 03-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Lecture 4

Presented ByDr. Shazzad Hosain

Asst. Prof. EECS, NSU

Page 2: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Agenda

• Control Flow Structures– FOR Loop– WHILE Loop– REPEAT-UNTIL Loop

• Load Effective Address (LEA) Instruction• Programming with Higher Level Structures

Page 3: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

FOR LoopWrite a program to display a row of 80 stars ‘*’

FOR 80 times DOdisplay ‘*’

END_FOR

MOV CX, 80 ; number of ‘*’ to displayMOV AH, 2 ; char display functionMOV DL, ‘*’ ; char to display

TOP:INT 21h ; display a starLOOP TOP ; repeat 80 times

Example 6-8: Assembly Language Programming

Page 4: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

WHILE LoopWrite a program to count the characters in an input line

Initialize count to 0Read a characterWHILE character <> carriage_return DO

count = count + 1read a character

END_WHILE

MOV DX, 0 ; DX counts the charactersMOV AH, 1 ; read char functionINT 21h ; read a char in AL

WHILE_:CMP AL, 0DH ; CR?JE END_WHILEINC DXINT 21hJMP WHILE_

END_WHILE:

WHILE condition DOstatements

END_WHILE

Example 6-9: Assembly Language Programming

Page 5: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

REPEAT LoopREPEAT statementsUNTIL condition

Write a program to read characters until a blank/space is read

REPEAT read a characterUNTIL character is a blank

MOV AH, 1 ; read char functionREPEAT:

INT 21h ; read a char in ALCMP AL, ‘ ‘ ; a blank?JNE REPEAT ; no, keep reading

Example 6-10: Assembly Language Programming

Page 6: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Road Map

• Control Flow Structures– IF-THEN– IF-THEN-ELSE– CASE– FOR Loop– WHILE Loop– REPEAT-UNTIL Loop

• Load Effective Address (LEA) Instruction• Programming with Higher Level Structures

Page 7: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Load Effective Address• The LEA instruction loads any 16 bit register with the data address

as determined• LEA vs. MOV

Page 8: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Load Effective Address Example• Write a program to exchange the contents of two memory

locations

Example 4-3: Intel Microprocessors – by Brey

Page 9: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

LEA vs. OFFSET Directive

• OFFSET functions only with simple operands such as LIST.• LEA functions with complex operands such as [DI],

LIST [SI] etc.• OFFSET is more efficient than LEA• LEA BX, LIST is costly than MOV BX, OFFSET LIST

Page 10: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Example• Write a program to print “Hello World”

.MODEL SMALL

.DATAPROMPT DB ‘Hello world’, 0DH, 0AH, ‘$’

.CODE

.STARTUP

; initialize DSMOV AX, @DATAMOV DS, AX; display opening messageMOV AH, 9 ; display string functionLEA DX, PROMPT ; get opening messageINT 21h ; display it.EXITEND

Page 11: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Road Map

• Control Flow Structures– IF-THEN– IF-THEN-ELSE– CASE– FOR Loop– WHILE Loop– REPEAT-UNTIL Loop

• Load Effective Address (LEA) Instruction• Programming with Higher Level Structures

Page 12: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Programming with High Level Structures

• Problem– Prompt the user to enter a line of text. On the

next line, display the capital letter entered that comes first alphabetically and the one that comes last. If no capital entered, display “No capital letters”.

Type a line of text:THE QUICK BROWN FOX JUMPEDFirst capital = B Last capital = X

Page 13: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Top-down Program Design

• Divide the problem into sub-problems1. Display the opening message2. Read and process a line of text3. Display the results

Page 14: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Start the Program

.MODEL SMALL

.STACK 100H

.DATAPROMPT 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

.STARTUP

Type a line of text:THE QUICK BROWN FOX JUMPEDFirst capital = B Last capital = X

Follows ‘Z’ in ASCII sequence

Precedes ‘A’ in ASCII sequence

@ABCDE………………………………..XYZ]FIRSTLAST

Page 15: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Step 1. Display the opening message

; initialize DSMOV AX, @DATAMOV DS, AX; display opening messageMOV AH, 9 ; display string functionLEA DX, PROMPT ; get opening messageINT 21h ; display it

.DATAPROMPT DB ‘Type a line of text’, 0DH, 0AH, ‘$’

Page 16: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Step 2: Read and Process a Line of Text

Read a characterWHILE character is not carriage return DO

IF character is a capital letter (*) THEN IF character precedes first capital THEN first capital = character END_IF IF character follows last capital THEN last capital = character END_IFEND_IFRead a character

END_WHILE

Line (*) is actually an AND condition:IF (‘A’ <= character) AND (character <= ‘Z’)

Page 17: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Step 2: Read and Process a Line of TextRead a characterWHILE character is not carriage return DO

IF character is a capital letter (*) THEN IF character precedes first capital THEN first capital = character END_IF IF character follows last capital THEN last capital = character END_IFEND_IFRead a character

END_WHILE

Line (*) is actually an AND condition:IF (‘A’ <= character) AND (character <= ‘Z’)

MOV AH, 1INT 21h

WHILE_:CMP AL, 0DHJE END_WHILECMP AL, ‘A’JNGE END_IFCMP AL, ‘Z’JNLE END_IFCMP AL, FIRST ; char < FIRST or ‘]’JNL CHECK_LASTMOV FIRST, AL

CHECK_LAST:CMP AL, LAST ; char > LAST or ‘@’JNG END_IFMOV LAST, AL

END_IF:INT 21HJMP WHILE_

END_WHILE:

@ABCDE………………………………..XYZ]FIRSTLAST

Page 18: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

Step 3: Display The ResultsIF no capitals were typed THEN display “no capitals”ELSE display first capital and last capitalEND_ID

MOV AH, 9 ; display string functionCMP FIRST, ‘]’JNE CAPS ; no, display resultsLEA DX, NOCAP_MSGJMP DISPLAY

CAPS:LEA DX, CAP_MSG

DISPLAY:INT 21H

.EXITEND

@ABCDE………………………………..XYZ]FIRSTLAST

Page 19: Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU

References

• Ch 6, Assembly Language Programming – by Charls Marut

• Section 4-3 of Intel Microprocessors – by Brey