Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language.

Slides:



Advertisements
Similar presentations
Computer Architecture and the Fetch-Execute Cycle
Advertisements

CompSci A Peek at the Lower Levels  It is good to have a sense of what happens at the hardware level  Not required for this course  It may.
The Little man computer
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
19-1 Programming… The Pencil and Paper Computer The Pencil & Paper Instruction Set: (table on p148) The Operand specifies a memory location.
Computer Systems. Computer System Components Computer Networks.
Low-Level Programming Languages
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Stored Program Concept: The Hardware View
Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers.
Chapter 7 Low-Level Programming Languages. 2 Chapter Goals List the operations that a computer can perform Discuss the relationship between levels of.
Basic Computer Organization, CPU L1 Prof. Sin-Min Lee Department of Computer Science.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Computer Systems CS208. Major Components of a Computer System Processor (CPU) Runs program instructions Main Memory Storage for running programs and current.
The CPU The Central Presentation Unit Language Levels Fetch execute cycle Processor speed.
Lecture 13 - Introduction to the Central Processing Unit (CPU)
Assembly & Machine Languages
What is TINY? 1.Base-10 Machine (emulator currently runs on Windows-based machines.) 2.Only 1,000 ‘bytes’ of memory 3.Only 5 registers 4.~26 machine instructions.
Lecture 3. Diff b/w RAM and Registers Registers are used to hold data immediately applicable to the operation at hand Registers are used to hold data.
The CPU The Central Presentation Unit Main Memory and Addresses Address bus and Address Space Data Bus Control Bus The Instructions set Mnemonics Opcodes.
Chapter 1: Basic Concepts
Computer Architecture and the Fetch-Execute Cycle
Computer Architecture and the Fetch-Execute Cycle
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Chapter 4 MARIE: An Introduction to a Simple Computer.
The Central Processing Unit (CPU) and the Machine Cycle.
Model Computer CPU Arithmetic Logic Unit Control Unit Memory Unit
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
Lecture 14 Today’s topics MARIE Architecture Registers Buses
Computer Operations A computer is a programmable electronic device that can store, retrieve, and process data Data and instructions to manipulate the data.
DH2T 34 – HNC Computer Architecture 1 Lecture 14 The Fetch-Decode-Execute Cycle [1]. © C Nyssen/Aberdeen College 2003 All images © C Nyssen/Aberdeen College.
D75P 34 – HNC Computer Architecture
Chapter 7 Low-Level Programming Languages (slides modified by Erin Chambers)
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
© GCSE Computing Candidates should be able to:  describe the characteristics of an assembler Slide 1.
Represents different voltage levels High: 5 Volts Low: 0 Volts At this raw level a digital computer is instructed to carry out instructions.
COMPILERS CLASS 22/7,23/7. Introduction Compiler: A Compiler is a program that can read a program in one language (Source) and translate it into an equivalent.
Computer Systems Organization
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Chapter 1 Getting Acquainted With Computers, Programs, and C++
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
CompSci Today’s topics Machine Architecture The basic machine Basic programming Assembler programming Upcoming Language Translation Reading Great.
Question What technology differentiates the different stages a computer had gone through from generation 1 to present?
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
Processor Fundamentals Assembly Language. Learning Objectives Show understanding of the relationship between assembly language and machine code, including.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Computer Operation. Binary Codes CPU operates in binary codes Representation of values in binary codes Instructions to CPU in binary codes Addresses in.
Lec 4-2 Five operations of the machine cycle Fetch- fetch the next program instruction from memory. (PC+1); instruction to IR Decode- decode the instruction.
Program to multiply 2 numbers 500-Input x 291-Store acc. as x 500-Input y 292-Store acc. as y 193-Load y in to acc. (0 on 1 st parse) 391-Add x to acc.
CPS4200 System Programming Spring 1 Systems Programming Chapter 1 Background I.
Little Man Computer Task 1 Last lesson you were asked to write a program to multiply two numbers together. The next slide has a working program to do this.
1 What we want: execute High Level Language (HLL) programs What we have: computer hardware (a glorified calculator)
The Little man computer
GCSE COMPUTER SCIENCE Computers 1.5 Assembly Language.
Control Unit Lecture 6.
Lecture 13 - Introduction to the Central Processing Unit (CPU)
Chapter 4 The Von Neumann Model
Starter Read the Feedback Click on Add new Feedback Open Realsmart
The Processor and Machine Language
CSCE Fall 2013 Prof. Jennifer L. Welch.
MARIE: An Introduction to a Simple Computer
Sequencing, Selection, and Loops in Machine Language
Computer Architecture
Processor Fundamentals
CSCE Fall 2012 Prof. Jennifer L. Welch.
Chapter 4: Representing instructions
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/09/29
Hmmm Assembly Language
Little Man Computer.
Presentation transcript:

Assembly Language Programming CS208

Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language operations and memory locations. Each assembly language is specific to a particular hardware architecture, and can only be used on a machine of that architecture. An assembly language program must be translated into machine code before it can be executed. The program that tells the computer how to perform the translation is called an assembler.

Assembly Language When a processor chip is designed, it is designed to understand and execute a set of machine code instructions (OpCodes) unique to that chip. One step up from machine code is assembly code. Each machine code instruction is given a mnemonic (name), so that it is easier for human beings to write code. There is generally a one-to-one correspondence between the assembly languages mnemonic instructions and the machine language numeric instructions.

Model Assembly Instructions Our assembly language instructions have two parts: –The operation code specifies the operation the computer is to carry out (add, compare, etc) –An address that allows the instruction to refer to a location in main memory The CPU runs each instruction in the program, starting with instruction 0, using the fetch-decode-execute cycle.

Review of the Fetch-Decode-Execute Cycle The CPU fetches the next instruction from the address contained in the Program Counter and places the instruction in the Instruction Register. –When a program starts, the program counter contains 0, so the instruction at address 0 is fetched. Immediately after the instruction fetch, the CPU adds 1 word to the contents of the Program Counter, so that it will contain the address of the next sequential instruction.

Review of the Fetch-Decode-Execute Cycle The CPU decodes the instruction in the Instruction Register and determines what operations need to be done and what the address is of any operand that will be used. The specified operation is executed (add, compare, etc). After execution of the instruction has been completed the cycle starts all over again (unless the instruction terminates the program).

CPU

CPU Registers The Instruction Register (IR) contains the actual instruction which is currently being executed by the CPU. The Status Register records the result of comparing the contents of register A with the contents of register B. The Program Counter (PC) contains the address of the next instruction to be executed by the program.

CPU Registers Registers A & B hold the operands for each arithmetic operation (ie. the values on which the operation will be performed). After the operation has been carried out, the result is always stored in Register B. Therefore, after an arithmetic operation has been performed, the second operand is no longer stored in Register B, because it has been overwritten by the result of the operation.

CPU Registers After a comparison has been done, the Status Register will hold a code that stores the results of the comparison. The results are coded as follows: -1 if (A < B) 0if (A = B) 1if (A > B)

Model Assembly Language Instructions OperationWhat it means to the CPU STPStop the program LDALoad register A with value from a specified memory location LDBLoad register B with value from a specified memory location STRStore register B value to a specified memory location INP Store data input by user to a specified memory location PNTPrint the value stored in a specified memory location to the screen

OperationWhat it means to the CPU JLTJump if less than (Status register = -1) to a specified memory location JGTJump if greater than (Status register = 1) to a specified memory location JEQJump if equal (Status register = 0) to a specified memory location JMPUnconditional jump to a specified memory location CMPCompare register A to register B and set Status Register value Model Assembly Language Instructions

OperationWhat it means to the CPU ADDAdd (register A + register B) and store sum in register B SUBSubtract (register A - register B) and store difference in register B MULMultiply (register A * register B) and store product in register B DIVDivide for quotient (register A/register B) and store quotient in register B MODDivide for remainder (register A/register B) and store remainder in register B Model Assembly Language Instructions

Steps to write Assembly Programs Create C++ Program (only the statements between the { and } brackets are needed) Translate each C++ statement to the equivalent assembly statement(s) Number the assembly language program starting from 0 Replace memory names by memory address numbers of empty memory cell Resolve jumps (replace with number of memory cell jumping to)

C++ to Assembly Language StatementAssembly equivalent #includenone void main()none constvalue in memory cell int, double, charaddress of memory cell cinINP coutPNT assignment (=)LDA Val1 val3 = val1 + val2LDB Val2 ADD STR Val3 }STP

Sample Program #1 Program #1. Write an assembly language program that will get a number as input from the user, and output its square to the user.

Sample Program #1 Step 1: Write an algorithm to describe the steps needed to solve our problem. Algorithm: 1. Input a number and store it in memory. 2. Compute the square by multiplying the number times itself. 3. Output the results.

Sample Program #1 Step 2: Write the C++ code { int Number, Square; cout << "Enter a number: "; cin >> Number; Square = Number * Number ; cout << Square; }

Sample Program #1 Step 3: Translate C++ code to assembly { cout << "Enter a number: "; cin >> Number; square = number * number; cout << Square; } INP number LDA number LDB number MUL STR square PNT square STP

Sample Program #1 Step 4: Number assembly code lines starting from 0 0INP number 1LDA number 2LDB number 3MUL 4STR square 5PNT square 6STP

Sample Program #1 Step 5: Replace memory names with empty memory locations after STP 0INP number 7 1LDA number 7 2LDB number 7 3MUL 4STR square 8 5PNT square 8 6STP

Sample Program #1 Step 6: Final Assembly code INP 7 LDA 7 LDB 7 MUL STR 8 PNT 8 STP

Running the Code on the Model Assembler Type the code on the previous slide into a file (use Notepad). Save the file as sample1.txt in the same directory as the assembler.exe file Double click assembler.exe Press ENTER Type the filename sample1.txt Press “r” to run

Before running the code, the screen will look like this: Your Assembly Code

After running the code, the screen will look like this: Results of Program Run

C++ Decisions to Assembly Language C++ Statement if ( Num < 10 ) cout << Num; Assembly equivalent LDA Num LDB Ten CMP[test condition] JLT Then block address JMP address of statement after Then block PNT Num [Then block]

C++ Decisions to Assembly Language Pascal Statement if ( Num < 10 ) cout << Num; else cout << “0”; Assembly equivalent LDA Num LDB Ten CMP[Test condition] JLT Then block address PNT Zero[Else block] JMP Address of statement after Then block PNT Num[Then block]

Sample Program #2 Program #2. Write an assembly program that will get a number from the user, and determine if the number is evenly divisible by 5. Output zero (false) if the number is NOT evenly divisible by 5 or one (true) if the number IS evenly divisible.

Sample Program #2 Step 1: Write the algorithm to describe the steps needed to solve our problem. 1. Read in a number and store it in memory. 2. Determine if input number is evenly divisible by Divide input number by 5 to get the remainder. 2.2 Compare remainder to 0. If remainder equals 0, the number is evenly divisible. If the remainder does not equal 0, the number NOT evenly divisible. 3. Output the results 3.1 If evenly divisible, output If NOT evenly divisible, output 0.

Sample Program #2 Step 2: Write the C++ code { const int Zero = 0; const int One = 1; const int Five = 5; int number, rem; cout << "Enter number: "; cin >> number; rem = number % Five; if (rem = Zero) cout << One; else cout << Zero; }

Sample Program #2 Step 3: Translate C++ code to Assembly cout << "Enter number: "; cin >> number;INP number LDA number rem = number % Five ;LDB Five MOD STR rem

Sample Program #2 Step 3: continued if (rem = Zero) LDA Zero cout << One; LDB rem elseCMP cout << Zero;JEQ then block address PNT Zero  else block JMP address after then block PNT One  then block }STP

Sample Program #2 Step 4: Number assembly code lines starting from 0 0 INP number 1 LDA number 2 LDB Five 3 MOD 4 STR rem 5 LDA Zero  condition 6 LDB rem 7 CMP 8 JEQ then block address 9 PNT Zero  else block 10 JMP address after then block 11 PNT One  then block 12 STP

Sample Program #2 Step 5: Replace names by cell numbers after STP 0 INP number 16 1 LDA number 16 2 LDB Five 13 3 MOD 4 STR rem 17 5 LDA Zero 14 6 LDB rem 17 7 CMP 8 JEQ then block address 9 PNT Zero JMP address after then block 11 PNT One STP 13 5  Five 14 0  Zero 15 1  One 16  number 17  rem

Sample Program #2 Step 5: Replace jumps by instruction numbers 0 INP 16 1 LDA 16 2 LDB 13 3 MOD 4 STR 17 5 LDA 14 6 LDB 17 7 CMP 8 JEQ address of then block 11 9 PNT 14  else block 10 JMP address after then block PNT 15  then block 12 STP

Sample Program #2 Step 6: Final Assembly code INP 16 LDA 16 LDB 13 MOD STR 17 LDA 14 LDB 17 CMP JEQ 11 PNT 14 JMP 12 PNT 15 STP 5 0 1

C++ Decisions to Assembly Language C++ Statement while ( Num < 10 ) cout << Num; Assembly equivalent LDA Num LDB Ten CMP  test condition JLT to While Block JMP to stmt after While Block PNT Num  While Block JMP to test condition  stmt after While Block

Sample Program #3 Program #3. Write a program to display a count by fives to 100.

Sample Program #3 Step 1: Write an algorithm to describe the steps needed to solve our problem 1.Set Count to start at 0 2.While Count is less than Add 5 to the Count and store the sum back into the Count 2.2 Display the Count

Sample Program #3 Step 2: Write C++ code { const int Five = 5; const int Hundred = 100; int Count = 0; while (Count < Hundred) { Count = Count + 5; cout >> Count; }

Sample Program #3 Step 3: Translate C++ code to assembly while (Count < Hundred) { Count = Count + 5; cout >> Count; } LDA Count  test condition LDB Hundred CMP JLT to while block JMP to stmt after while block LDB Five  while block ADD STR Count PNT Count JMP to test condition  stmt after while block

Sample Program #3 Step 4: Number assembly code lines from 0 0LDA Count  test condition 1LDB Hundred 2CMP 3JLT to while block 4JMP to stmt after while block 5LDB Five  while block 6ADD 7STR Count 8PNT Count 9JMP to test condition 10 STP  stmt after while block

Sample Program #3 Step 5: Replace memory names with empty memory locations after STP 0LDA Count 13 1LDB Hundred 12 2CMP 3JLT to while block 4JMP to stmt after while block 5LDB Five 11 6ADD 7STR Count 13 8PNT Count 13 9JMP to test condition 10 STP 11 5  Five  Hundred 13 0  Count

Sample Program #3 Step 5: Replace memory names with empty memory locations after STP 0LDA 13  test condition 1LDB 12 2CMP 3JLT to while block 5 4JMP stmt after while block 10 5LDB 11  while block 6ADD 7STR 13 8PNT 13 9JMP to test condition 0 10 STP  stmt after while block

Sample Program #3 Final Assembly Code: LDA 13 LDB 12 CMP JLT 5 JMP 10 LDB 11 ADD STR 13 PNT 13 JMP 0 STP