Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 Assembly Language. Problems with Machine Language Uses binary - No English-like words to make it more readable Uses binary - No English-like.

Similar presentations


Presentation on theme: "Computer Science 101 Assembly Language. Problems with Machine Language Uses binary - No English-like words to make it more readable Uses binary - No English-like."— Presentation transcript:

1 Computer Science 101 Assembly Language

2 Problems with Machine Language Uses binary - No English-like words to make it more readable Uses binary - No English-like words to make it more readable Only numeric memory addresses - Can not name an instruction or data location Only numeric memory addresses - Can not name an instruction or data location Difficult to change - If we leave out one instruction, all addresses from that point on will be incorrect. Difficult to change - If we leave out one instruction, all addresses from that point on will be incorrect. Difficult to create data - Must use internal binary representation Difficult to create data - Must use internal binary representation

3 Assembly Language to the Rescue! Symbolic operation codes: Load, Store, Jump, Compare, etc. Symbolic operation codes: Load, Store, Jump, Compare, etc. Symbolic names (user defined) for memory addresses Load Pay Symbolic names (user defined) for memory addresses Load Pay Pseudo-operations provide services such as data generation. Pseudo-operations provide services such as data generation.

4 Assembler An assembler is a program that takes a source code program written in assembly language and converts it to a machine language object file. An assembler is a program that takes a source code program written in assembly language and converts it to a machine language object file. Essentially a line by line translation. Essentially a line by line translation. Assembly lang Source file Assembler Machine lang Object file

5 Labels: Giving names to locations. We can attach a symbolic name to any instruction or data location by beginning the line with a label with that name. We can attach a symbolic name to any instruction or data location by beginning the line with a label with that name. A label consists of the name followed by a colon. A label consists of the name followed by a colon. You use labels in all of the places that addresses would occur in machine language; i.e. you no longer use numeric addresses. You use labels in all of the places that addresses would occur in machine language; i.e. you no longer use numeric addresses. –Address fields in instructions  Operands of computations  Targets of branch instructions

6 Pseudo-operations Pseudo-operations are not statements that are converted to machine code. They are used to request services of the assembler. Performed only when program assembled. Pseudo-operations are not statements that are converted to machine code. They are used to request services of the assembler. Performed only when program assembled. Hypothetical Machine Pseudo-Ops: Hypothetical Machine Pseudo-Ops: All of the pseudo-ops begin with a period..begin -- required to mark beginning.begin -- required to mark beginning.end -- required to mark end.end -- required to mark end.data 30 -- puts a value at the memory location; value in decimal assembler converts to binary.data 30 -- puts a value at the memory location; value in decimal assembler converts to binary

7 Example: Add X to Y and Store in Z Z = X + Y.begin load X add Y store Z halt X:.data 33 Y:.data 40 Z:.data 0.end

8 Developing Assembly Programs First write your algorithm in pseudocode. First write your algorithm in pseudocode. Then convert to assembly. Then convert to assembly. Convert computations using labeled memory locations and the register for variables - document these. Convert computations using labeled memory locations and the register for variables - document these. We’ll look at techniques for converting common control flow constructs to assembly language. We’ll look at techniques for converting common control flow constructs to assembly language.

9 Converting Set (Assignment) Statements Set to Set to Semantics: Compute value of the expression Give this value to the variable Semantics: Compute value of the expression Give this value to the variable Assembly language: Accumulate value of expression in R Store the value in the variable’s location Assembly language: Accumulate value of expression in R Store the value in the variable’s location

10 Example Set X to 2Y + Z – 5 Set X to 2Y + Z – 5 load Y add Y add Z subtract FIVE store X … X:.data 0 Y:.data 20 Z:.data 30 FIVE:.data 5 … load Y add Y add Z subtract FIVE store X … X:.data 0 Y:.data 20 Z:.data 30 FIVE:.data 5 …

11 Converting If-statements If X  Y then Statements A Statements B If X  Y then Statements A Statements B load X load X compare Y compare Y jumpgt BSpot jumpgt BSpot … Translation of A... … Translation of A... BSpot: … Translation of B A Code B Code T F Cond

12 If-statement example User inputs number. If number is greater than 5 we output the number. User inputs number. If number is greater than 5 we output the number. Pseudocode: Get N If N>5 then Print N Stop Pseudocode: Get N If N>5 then Print N Stop.begin.begin in N in N load N load N compare FIVE compare FIVE jumpgt DONE jumpgt DONE jumpeq DONE jumpeq DONE out N out N DONE: halt N:.data 0 FIVE:.data 5.end

13 Converting If-else-statements If X  Y then Statements A If X  Y then Statements A else Statements B else Statements B Statements C Statements C load X load X compare Y compare Y jumpgt Else jumpgt Else … Translation of A... … Translation of A... Else: … Translation of B … … Translation of C … Translation of C jump CSpot CSpot: Cond A Code C Code TF BCode

14 Converting While-statements While X  Y Do Statements A While X  Y Do Statements A Statements B Statements B load X load X compare Y compare Y jumpgt BSpot jumpgt BSpot … Translation of A... … Translation of A... BSpot: … Translation of B … jump WHILE WHILE: B Code A Code T F Cond

15 While-statement example Program outputs sum of 10 numbers entered by the user. Program outputs sum of 10 numbers entered by the user. Pseudocode: Set Ct to 10 Set Sum to 0 While Ct>0 Get Num Set Sum to Sum+Num Set Ct to Ct-1 Print Sum Stop Pseudocode: Set Ct to 10 Set Sum to 0 While Ct>0 Get Num Set Sum to Sum+Num Set Ct to Ct-1 Print Sum Stop.begin.begin load TEN store CT load TEN store CT clear SUM clear SUM WHILE: load CT compare ZERO compare ZERO jumpeq PRNT jumpeq PRNT in NUM in NUM load NUM load NUM add SUM add SUM store SUM store SUM decrement CT decrement CT jump WHILE jump WHILE PRNT: out SUM PRNT: out SUM halt halt

16 While-statement example (cont.).begin load TEN store CT load TEN store CT clear Sum clear Sum WHILE: load CT compare ZERO compare ZERO jumpeq PRNT jumpeq PRNT in NUM in NUM load NUM load NUM add SUM add SUM store SUM store SUM decrement CT decrement CT jump WHILE jump WHILE PRNT: out SUM PRNT: out SUM halt halt TEN:.data 10 CT:.data 0 SUM:.data 0 NUM:.data 0 ZERO:.data 0.end

17


Download ppt "Computer Science 101 Assembly Language. Problems with Machine Language Uses binary - No English-like words to make it more readable Uses binary - No English-like."

Similar presentations


Ads by Google