1 / 45

Assembly Language Programming CS208

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

adah
Download Presentation

Assembly Language Programming CS208

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly Language Programming CS208

  2. 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.

  3. 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.

  4. 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 thatallows 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.

  5. 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.

  6. Review of theFetch-Decode-Execute Cycle • TheCPU 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).

  7. CPU

  8. 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.

  9. 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 overwrittenby the result of the operation.

  10. 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) 0 if (A = B) 1 if (A > B)

  11. Model Assembly Language Instructions OperationWhat it means to the CPU STP Stop the program LDA Load register A with value from a specified memory location LDB Load register B with value from a specified memory location STR Store register B value to a specified memory location INP Store data input by user to a specified memory location PNT Print the value stored in a specified memory location to the screen

  12. Model Assembly Language Instructions OperationWhat it means to the CPU JLT Jump if less than (Status register = -1) to a specified memory location JGT Jump if greater than (Status register = 1) to a specified memory location JEQ Jump if equal (Status register = 0) to a specified memory location JMP Unconditional jump to a specified memory location CMP Compare register A to register B and set Status Register value

  13. Model Assembly Language Instructions OperationWhat it means to the CPU ADD Add (register A + register B) and store sum in register B SUB Subtract (register A - register B) and store difference in register B MUL Multiply (register A * register B) and store product in register B DIV Divide for quotient (register A/register B) and store quotient in register B MOD Divide for remainder (register A/register B) and store remainder in register B

  14. 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)

  15. C++ to Assembly Language StatementAssembly equivalent #include none void main() none const value in memory cell int, double, char address of memory cell cin INP cout PNT assignment (=) LDA Val1 val3 = val1 + val2 LDB Val2 ADD STR Val3 } STP

  16. 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.

  17. 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.

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

  19. { 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 #1Step 3: Translate C++ code to assembly

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

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

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

  23. 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

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

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

  26. C++ Decisionsto Assembly Language C++ Statement if ( Num < 10 ) cout << Num; Assembly equivalent LDA Num LDB Ten CMP [test condition] JLTThen block address JMPaddress of statement after Then block PNT Num[Then block]

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

  28. 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.

  29. 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 5. 2.1 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 1. 3.2 If NOT evenly divisible, output 0.

  30. 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; }

  31. 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

  32. 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

  33. 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

  34. 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 14 10 JMP address after then block 11 PNT One 15 12 STP 13 5  Five 14 0  Zero 15 1  One 16  number 17  rem

  35. 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 block11 9 PNT 14 else block 10 JMP address after then block 12 11 PNT 15 then block 12 STP 13 5 14 0 15 1 16 17

  36. 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

  37. 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

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

  39. 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 100 2.1 Add 5 to the Count and store the sum back into the Count 2.2 Display the Count

  40. 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; } }

  41. 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

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

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

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

  45. 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 5 100 0

More Related