170 likes | 307 Vues
Assembly language provides a solution to the inherent difficulties of machine language, which uses binary and numeric memory addresses that are hard to read and modify. This guide introduces symbolic operation codes, labels, and pseudo-operations, which simplify coding and data manipulation. By learning how to convert pseudocode into assembly language, readers will be able to efficiently create programs while addressing the issues of instruction omission and memory management. Gain a better understanding of creating assembly programs, handling control flow constructs, and building effective algorithms.
E N D
Computer Science 101 Assembly Language
Problems with Machine Language • Uses binary - No English-like words to make it more readable • 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 create data - Must use internal binary representation
Assembly Language to the Rescue! • Symbolic operation codes: Load, Store, Jump, Compare, etc. • Symbolic names (user defined) for memory addresses Load Pay • Pseudo-operations provide services such as data generation.
Assembler Assembly lang Source file Machine lang Object file 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. • Essentially a line by line translation.
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. • 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. • Address fields in instructions • Operands of computations • Targets of branch instructions
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. • Hypothetical Machine Pseudo-Ops: All of the pseudo-ops begin with a period. .begin -- required to mark beginning .end -- required to mark end .data 30 -- puts a value at the memory location; value in decimal assembler converts to binary
Example: Add X to Y and Store in ZZ = X + Y .begin load X add Y store Z halt X: .data 33 Y: .data 40 Z: .data 0 .end
Developing Assembly Programs • First write your algorithm in pseudocode. • Then convert to assembly. • 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.
Converting Set (Assignment) Statements • Set <variable> to <expression> • 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
Example • Set X to 2Y + Z – 5 • load Y add Y add Z subtract FIVE store X …X: .data 0Y: .data 20Z: .data 30FIVE: .data 5…
If XY then Statements AStatements B load X compare Y jumpgtBSpot …Translation of A ... BSpot: …Translation of B Converting If-statements T Cond A Code F B Code
User inputs number. If number is greater than 5 we output the number. Pseudocode: Get N If N>5 then Print N Stop .begin in N load N compare FIVE jumpgt DONE jumpeq DONE out N DONE: halt N: .data 0 FIVE: .data 5 .end If-statement example
If XY then Statements A else Statements B Statements C load X compare Y jumpgt Else …Translation of A ... Else: …Translation of B … …Translation of C jump CSpot CSpot: Converting If-else-statements F T Cond BCode A Code C Code
While XY Do Statements A Statements B load X compare Y jumpgt BSpot …Translation of A ... BSpot: …Translation of B … WHILE: jump WHILE Converting While-statements T Cond A Code F B Code
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 .begin load TEN store CT clear SUM WHILE: load CT compare ZERO jumpeq PRNT in NUM load NUM add SUM store SUM decrement CT jump WHILE PRNT: out SUM halt While-statement example
.begin load TEN store CT clear Sum WHILE: load CT compare ZERO jumpeq PRNT in NUM load NUM add SUM store SUM decrement CT jump WHILE PRNT: out SUM halt TEN: .data 10 CT: .data 0 SUM: .data 0 NUM: .data 0 ZERO: .data 0 .end While-statement example (cont.)
Cows, horses, supplies,... Man, I can't keep track of everything!