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

Slides:



Advertisements
Similar presentations
Assembly Language – 1.
Advertisements

Fundamentals of Computer and programming in C Programming Languages, Programs and Programming Rohit Khokher.
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.
Instruction Set Architecture
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#3) By Dr. Syed Noman.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
8086 Assembly Language Programming I
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Lab6 – Debug Assembly Language Lab
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Microcomputer & Interfacing Lecture 3
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#1) By Dr. Syed Noman.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Assembly & Machine Languages
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Assembly Programming Timothy C. Rice Jr., MIT. OUTLINE Basic Structure Exit to Dos Print Character Clear Screen Change BG & FG Color Set Curser Location.
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 ;
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Chapter 1: Basic Concepts
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.
Debug and Assembler By, B.R.Chandavarkar Lect. COMP Department NITK, Surathkal.
Programming With C.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
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.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
8086/8088 Instruction Set, Machine Codes and Addressing Modes.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
F453 Module 8: Low Level Languages 8.2: Features of Low-Level Languages.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Assembly language programming
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Control Unit Lecture 6.
Assembly Language programming
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Computer Organization & Assembly Language Chapter 3
Additional Assembly Programming Concepts
Lecture 4 Control Flow Structures (LOOPS)
Microprocessor and Assembly Language
(The Stack and Procedures)
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(The Stack and Procedures)
Symbolic Instruction and Addressing
Chapter 6 –Symbolic Instruction and Addressing
Process.
Chapter 6 Programming the basic computer
(The Stack and Procedures)
Presentation transcript:

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

Assembly language An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture. This representation is usually defined by the hardware manufacturer, and is based on abbreviations (called mnemonics) that help the programmer remember individual instructions, registers, etc. An assembly language is thus specific to certain physical or virtual computer architecture (as opposed to most high-level languages, which are usually portable).

How to learn programming C –Concept L – Logic thinking P – Practice Concept – we must learn the basic syntax, such as how a program statement is written Logic thinking – programming is problem solving so we must think logically in order to derive a solution Practice – write programs

Instruction Set A collection of assembly/machine language instructions that the processor can execute. Types of instructions: Transfer (MOV, PUSH, POP etc) Arithmetic (ADD, SUB, MUL etc) Logical (AND, OR, NOT etc Control Transfer (CALL, JMP etc) Miscelleneous (NOP, INT, LEA)

Format of Assembly language Statement General format for an assembly language statement Label Instruction Comment Start: Mov AX, BX ; copy BX into AX Start is a user defined name. put in a label in your statement when necessary!!!! The symbol : is used to indicate that it is a label In instruction, ‘mov’ is operation code (opcode) and ‘AX’ and ‘BX’ are operands.

#-operands instruction Mov ax, bx 1-operand instruction Inc dl 0-operand instruction Nop

Assignment Statement Assignment In Java, assignment takes the form: x = 42 ; y = 24; z = x + y; In assembly language we carry out the same operation but we use an instruction to denote the assignment operator (“=” in Java). mov x, 42 mov y, 24 add z, x add z, y

Assignment Statement The mov instruction carries out assignment in 8086 assembly language. It allows us to place a number in a register or in a memory location (a variable) i.e. it assigns a value to a register or variable. Example: Store the ASCII code for the letter A in register bx. A has ASCII code 65D (01000001B, 41H) The following mov instruction carries out the task: mov bx, 65d

Assignment Statement We could also write it as: mov bx, 41h or mov bx, 01000001b mov bx, ‘A’ All of the above are equivalent. They each carry out exactly the same task, namely the binary number representing the ASCII code of A is copied into the bx register.

Assembly program development .asm Object file .obj Executable file .exe link Assemble

Input and Output in 8086 Assembly Each microprocessor provides instructions for I/O with the devices that are attached to it, e.g. the keyboard and screen. The 8086 provides the instructions “in” for input and “out” for output. These instructions are quite complicated to use, so we usually use the operating system to do I/O instead. The operating system provides a range of I/O subprograms, in much the same way as there is an extensive library of subprograms available to the C programmer. In C, to perform an I/O operation, a subprogram is called using its name, e.g. putchar() , printf() , getchar() . In addition, for example the character to be displayed by putchar() is passed as a parameter to the subprogram e.g. putchar(c).

Software Interrupts In assembly language we must have a mechanism to call the operating system to carry out I/O. In addition we must be able o tell the operating system what kind of I/O operation we wish to carry out, e.g. to read a character from the keyboard, to display a character or string on the screen or to do disk I/O. Finally, we must have a means of passing parameters to the operating subprogram.

“int” instruction In 8086 assembly language, operating system subprograms are not called by name, instead, by using a software interrupt mechanism An interrupt signals the processor to suspend its current activity (i.e. running your program) and to pass control to an interrupt service program (i.e. part of the operating system). A software interrupt is one generated by a program (as opposed to one generated by hardware). The 8086 int instruction generates a software interrupt. It uses a single operand which is a number indicating which MSDOS subprogram is to be invoked. When the I/O operation is finished, the interrupt service program terminates and our program will be resumed at the instruction following int.

int 21h and its services For I/O and some other operations, the number used is 21h . Thus, the instruction int 21h transfers control to the operating system, to a subprogram that handles I/O operations. This subprogram handles a variety of I/O operations by calling appropriate subprograms. This means that particular I/O operation (e.g. read a character, display a character) must be specified. This is done by placing a specific number in a register. The ‘ah’ register is used to pass this information. For example, the subprogram to display a string is subprogram number 9h.

String Output Service 9h to display strings which are terminated by the ‘$’ character. In order to use it, we must: Ensure the string is terminated with the ‘$’ character. Specify the starting address of the string in the dx register. Specify the string output subprogram by storing 9h in ah. Use int 21h to call MS-DOS to execute subprogram 9h.

Defining String Variables The following 3 definitions are equivalent ways of defining a string “abc“: style1 db “abc” ; string constant style2 db ‘a’, ‘b’, ‘c’ ; character constants style3 db 97, 98, 99 ; ASCII codes

A program template .model small .stack 100h .code start: ; < the code/statements are here > mov ax, 4c00h ; return to OS int 21h end start

EP#1: A Simple Assembly Program .model small .stack 100h .data mesg db “Welcome to Assembly Language $” .code start: mov ax, @data mov ds, ax ;initialize data segment mov ah, 9h ; service of interrupt 21 to print a message lea dx, mesg ; load effective address int 21h mov ax, 4c00h ; terminating program service end start end

Program elements

Character Output The task here is to display a single character on the screen. There are three elements involved in carrying out this operation using the int instruction: We specify the character to be displayed. This is done by storing the character’s ASCII code in a specific 8086 register. In this case we use the dl register, i.e. we use dl to pass a parameter to the output subprogram. We specify which of MS-DOS’s I/O subprograms we wish to use. The subprogram to display a character is subprogram number 2h . This number is stored in the ah register. We request MS-DOS to carry out the I/O operation using the int instruction. This means that we interrupt our program and transfer control to the MS-DOS subprogram that we have specified using the ah register.

Character Output Example : Write a code fragment to display the character ’a’ on the screen: C version: putchar( ‘a’ ) ; 8086 version: mov dl, ‘a’ ; dl = ‘a‘ mov ah, 2h ; character output subprogram int 21h ; call ms-dos output character As you can see, this simple task is quite complicated in assembly language.

EP#2: Displaying a character from a register .model small .stack 100h .data msg1 db "Displaying a character from register:$” .code mov ax, @data mov ds, ax mov ah,9 ; display message mov dx, offset msg1 ; lea dx, msg1 int 21h mov ah,2 ; display character mov dl, 65 mov ax, 4c00h end

EP#3: Displaying a character from a register on newline .model small .stack 100h .data msg1 db "Displaying a character from register:”,13,10, ‘$’ .code mov ax, @data mov ds, ax mov ah,9 ; display message mov dx, offset msg1 ; lea dx, msg1 int 21h mov ah,2 ; display character mov dl, 65 mov ax, 4c00h end

Assignment AP#1, AP#2 Write a program to display ‘Salam’ using (a) character output and (b) using string output. Write a program to display the message ‘Ding! Ding! Ding!’ and output ASCII code 7 three times. (ASCII code 7 is the Bel character. It causes your machine to beep! ).

Character Input with echo Example : Write a code fragment to input a character from the keyboard: C version: getche( ) ; 8086 version: mov ah, 1 ; character input subprogram int 21h ; call ms-dos output character The ASCII code of the character will be moved in ‘al’ register.

EP#4: Program to read a character and display it .model small .stack 100h .code start: mov ah, 1h ; keyboard input subprogram int 21h ; read character into al mov dl, al mov ah, 2h ; display subprogram int 21h ; display character in dl mov ax, 4c00h ; return to ms-dos int 21h end start

Assignment: AP#3 Write a program to beep, display ‘?’ as a prompt, read a character and display it on a new line.

EP#5: Input character and display with messages Title ask user for 1 character and display it again on screen with appropriate messages . .model small .stack 100h .data msg1 db "Input a character: $" msg2 db 13,10,"You entered: $" .code mov ax, @data mov ds, ax mov ah,9 ; display message lea dx, msg1 int 21h mov ah, 1 int 21h ; my character in al register mov bh, al mov ah,9 lea dx, msg2 int 21h mov ah,2 ; display character mov dl, bh mov ax, 4c00h end

Character Input without echo Example : Write a code fragment to input a character from the keyboard without displaying it: C version: getch( ) ; 8086 version: mov ah, 8 ; character input subprogram int 21h ; call ms-dos output character The ASCII code of the character will be moved in ‘al’ register.

Assignment: AP#4 Write a program to display a message “Input a secret character:” and then input the character and display the character after displaying the message on newline “hmm.. You actually input:”.