CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)

Slides:



Advertisements
Similar presentations
DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
Advertisements

ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Flow Control Instructions
Kip Irvine: Assembly Language for Intel-Based Computers
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Program.-(4)* Write a program for input two integer number with message and display their sum. Algorithm steps Algorithm steps 1.Display message for input.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Topics Control Flow Structure – Conditional Jump – Unconditional Jump Control Flow Structures – IF-THEN – IF-THEN-ELSE – CASE Branches with Compound Conditions.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
BIOS1 Basic Input Output System BIOS BIOS refers to a set of procedures or functions that enable the programmer have access to the hardware of the computer.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Binary Number Output To display a number in binary format, a program looks at each bit in the number and sends the ASCII equivalent of a ‘1’ (31h) or a.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7) By Dr. Syed Noman.
Program Structure Example for Data Segment CRLF EQU 0DH, 0AH PROMPT DB 'Enter a digit between 0 and 9', 0 VAR1 DB ? ARRAY DW 1234h, 23h, 0FF54h.
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Computer Science 210 Computer Organization
ICS103 Programming in C Lecture 3: Introduction to C (2)
Additional Assembly Programming Concepts
Lecture 4 Control Flow Structures (LOOPS)
Computer Organization & Assembly Language
(The Stack and Procedures)
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
BIC 10503: COMPUTER ARCHITECTURE
Computer Science 210 Computer Organization
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(The Stack and Procedures)
Shift & Rotate Instructions)
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
High-level language structures
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
Some Assembly (Part 2) set.html.
(The Stack and Procedures)
Jump & Loop instructions
By Nasser Halasa Assembly Language.
Part VI Looping Structures
Presentation transcript:

CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman

Subprograms/Procedures A subprogram allows us to give a name to a group of instructions and to use that name when we wish to execute those instructions, instead of having to write the instructions again. For example, the instructions to display a character could be given the name putc (or whatever you choose). Then to display a character you can use the name putc which will cause the appropriate instructions to be executed.

Call Instruction Call putc ; Display character in dl This is referred to as calling the subprogram. In 8086 assembly language, the instruction call is used to invoke a subprogram, so for example, a putc subprogram would be called as follows: Call putc ; Display character in dl The process of giving a group of instructions a name is referred to as defining a subprogram. This is only done once.

Ret instruction The ret instruction terminates the subprogram and arranges for execution to resume at the instruction following the call instruction.

Procedures for character and string output puts: ; display a string terminated by $ ; ax contains address of string mov dx, ax ; copy address to dx for ms-dos mov ah, 9h int 21h ; call ms-dos to output string ret putc: ; display character in al mov dl, al ; copy al to dl for ms-dos mov ah, 2h int 21h

Procedure for character input getc: ;read character into al mov ah, 1h int 21h ret

Important point about sub-programs We usually refer to that part of a program where execution begins as the main program . In practice, programs consist of a main program and a number of subprograms. It is important to note that subprograms make our programs easier to read, write and maintain even if we only use them once in a program. Note : Subprograms are defined after the code to terminate the program, but before the end directive. If we placed the subprograms earlier in the code, they would be executed without being called (execution would fall through into them). This should not be allowed to happen.

Printing string using procedure The following program, displays the message ‘Hello World’, and uses the equ directive. .model small .stack 100h .data CR equ 13d LF equ 10d message db ‘Hello World’, CR, LF, ‘$’ .code start: mov ax, @data mov ds, ax mov dx, offset message call puts ; display message mov ax, 4c00h int 21h ; User defined subprograms puts: ; display a string terminated by $ ; dx contains address of string mov ah, 9h int 21h ; output string ret end start

Symbolic constants A symbolic constant is created by associating an identifier with an integer expression or some text. Symbols do not reserve storage. They are used only by the assembler when scanning a program, and they cannot change at run time. The EQU directive associates a symbolic name with an integer expression or some arbitrary text.

Character Conversion: Uppercase to Lowercase & vice-versa To convert an uppercase letter to lowercase, we note that ASCII codes for the uppercase letters ‘A’ to ‘Z’ form a sequence from 65 to 90. The corresponding lowercase letters ‘a‘ to ‘z’ have codes in sequence from 97 to 122. We say that ASCII codes form a collating sequence and we use this fact to sort textual information into alphabetical order. To convert from an uppercase character to its lowercase equivalent, we add 32 to the ASCII code of the uppercase letter to obtain the ASCII code of the lowercase equivalent. To convert from lowercase to uppercase, we subtract 32 from the ASCII code of the lowercase letter to obtain the ASCII code of the corresponding uppercase letter. The number 32 is obtained by subtracting the ASCII code for ‘A’ from the ASCII code for ‘a’ i.e. (‘A’ - ‘a’ = 97 - 65 = 32).

Character conversion program with procedures jne finish ; do loop body mov ax, offset msg1 call puts ; prompt for letter call getc ; read character mov bl, al ; save character in bl add bl, 32d ; convert to lowercase mov ax, offset result call puts ; display result message mov al, bl call putc ; display lowercase letter mov ax, offset msg0 call puts ; prompt to continue call getc ; read reply mov reply, al ; save character in reply jmp readloop ; repeat loop test finish: mov ax, 4c00h int 21h ; return to ms-dos ; user defined subprograms should be ;defined here end start ; char4.asm: character conversion: ;upper to lowercase .model small .stack 100h CR equ 13d LF equ 10d .data reply db ‘y’ msg0 db CR, LF, ‘Enter y to continue: $’ msg1 db CR, LF, ‘Enter an uppercase letter: $’ result db CR, LF, ‘The lowercase equivalent is: $’ .code ; main program start: mov ax, @data mov ds, ax readloop: cmp reply, ‘y’ ; while (reply == ‘y‘)

Program (char4.asm) output Executing this program produces as output: Enter an uppercase letter: G The lowercase equivalent is: g The string result is defined to begin with the “Return” and “Line-feed” characters so that it will be displayed on a new line. An alternative would have been to include the two characters at the end of the string msg1, before the ‘$’ character, e.g. msg1 db ‘Enter an uppercase letter: ’,CR, LF, ‘$’ After displaying msg1, as defined above, the next item to be displayed will appear on a new line.

Assignment 2b Write a program that takes a character from the keyboard and converts it to the opposite case (lowercase to uppercase or vice versa) if it is an English alphabet; otherwise the program prints the message “The character is not an English alphabet!”

Guessing Game Program Write a code fragment to read a character entered by the user and compare it to the character ‘A’. Display an appropriate message if the user enters an ‘A’. This code fragment is the basis of a guessing game program.

Guessing Game Program: C code fragment C version: printf(“Guessing game: Enter a letter (A to Z): “); c = getchar() ; if ( c == ‘A’ ) printf(“You guessed correctly !! “); else printf(“Sorry incorrect guess “) ;

Guessing Game Program : Assembly code fragment mov ax, offset prompt ; prompt user call puts call getc ; read character cmp al, ‘A’ ; compare it to ‘A’ jne is_not_an_a ; jump if not ‘A’ mov ax, offset yes_msg ; if action call puts ; display correct guess jmp end_else ; skip else action is_not_an_A: ; else action mov ax, offset no_msg call puts ; display wrong guess end_else:

Guessing Game Program (complete) .model small .stack 100h CR equ 13d LF equ 10d .data prompt db “Guessing game: Enter a letter (A to Z): $” yes_msg db CR, LF,“You guessed correctly !! $“ no_msg db CR, LF,“Sorry incorrect guess $” .code start: mov ax, @data mov ds, ax mov ax, offset prompt call puts ; prompt for input call getc ; read character cmp al, ‘A’ jne is_not_an_a ; if (al != ‘A’) skip action mov ax, offset yes_msg ; if action call puts ; display correct guess jmp end_else1 ; skip else action is_not_an_A: ; else action mov ax, offset no_msg call puts ; display wrong guess end_else1: finish: mov ax, 4c00h int 21h ; User defined subprograms ; < puts getc defined here> end start

Assignment 2c Write an assembly language program that prints the previous and the forward character for a digit entered within the range of 0-9. use messages for -1 and 10.

Assignment 2d Write an assembly language program that input two digits within the range of 0 to 9 and print the sum if the sum is within the range of 1 to 9, otherwise prints the message “Out of range!”. The program should ask the user for the repetition of the same process.