Download
h1 assembly language part 2 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 4 PowerPoint Presentation

Chapter 4

67 Views Download Presentation
Download Presentation

Chapter 4

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. H1 Assembly Language: Part 2 Chapter 4

  2. Contains the absolute address of the memory location it accesses. ld instruction: 0000 000000000100 Direct instruction Absolute address

  3. Shorthand notation for ld x ac = mem[x]; where 0 <= x <= 4095

  4. code stack sp stack frame for main()

  5. Stack instructions • push pushes the ac register contents onto the top of the stack. • pop removes the value of top of the stack and loads it into the ac register. • swap exchanges the values in the ac and sp registers. • sp register points to the top of the stack. • sp is predecremented on a push.

  6. An immediate instruction contains the operand—not the operand address as in the direct instructions. Because it is in the instruction, the operand is “immediately” available. Immediate instructions

  7. Machine code: 1000 000000000001 ldc 1 Loads 1 (the operand in the instruction itself) into the ac register (zero extends operand to 16 bits).

  8. Execution of ldc 1

  9. What does this program do?

  10. What an assembler does • Translates mnemonics to binary opcodes. • Translate labels to binary addresses. • Translates numbers to binary. • Translates strings to ASCII codes. ld x 0000 0000 0000 0100 ldc x 1000 0000 0000 0100 ldc 5 1000 0000 0000 0101 dw ‘A’ 0000000001000001

  11. ldc w 1000 000000001111 Address of w This instruction loads the address of w into the ac register.

  12. This instruction loads the ASCII code for ‘A’ into the ac register. ldc ‘A’ 1000 000001000001 ASCII code for ‘A’

  13. Uses of the ldc instruction

  14. aloc and dloc instructions • alloc 2 subtracts 2 from sp register, reserving two slots on the stack • dloc 2 adds 2 to the sp register, deallocating two slots on the stack.

  15. Effect of aloc 2

  16. Running sim without the debugger

  17. dout, hout, aout do not output newlines Output: 650041A

  18. Output of previous program is

  19. ldc ‘\n’ aout To output a newline character, use the aout instruction:

  20. When an input instruction is executed, the system waits until the user enters the required input on the keyboard.

  21. Process State Diagram: stop IO blocking run start wait

  22. sin and sout • sin and sout, respectively, input and output to the memory location pointed to by the ac register. • sout continues until a null character is reached and outputed. • Be sure to use double-quoted strings with sout (because they are null terminated).

  23. How does a jump instruction work? Machine code ja 1 9001 The execution of this instruction loads 1 into the pc register.

  24. ldc 5 jz xxx ; no jump ldc 5 jnz yyy ; jump occurs Conditional Jump Instructions

  25. Infinite loop

  26. Sim’s response to infinite loop

  27. A loop whose number of iterations depends on a counter. The program on the next slide computes 20 + 19 + …+ 1 using a count-controlled loop.. Count-controlled loop

  28. 7 instructions inside loop

  29. Loop With Jump to Test: start: ja test loop: ld sum add count st sum test: ld count sub @1 st count jnz loop done: ldc msg sout ld sum dout ldc '\n' aout halt end start ; data count: dw 21 msg: dw “sum = “ sum: dw 0 @1: dw 1 Original Problem: Add the first twenty positive integers Programming Logic Problem: Counter starts at 21 so you might think we'll end up adding the first twenty-one positive integers Note: 7 instructions inside loop

  30. Loop With Jump to Test Rewrite: start: ja test loop: ld sum add count st sum ld count sub @1 st count test: ld count jnz loop done: ldc msg sout ld sum dout ldc '\n' aout halt end start ; data count: dw 20 msg: dw “sum = “ sum: dw 0 @1 dw 1 Original Problem: Add the first twenty positive integers Programming Logic Problem: Gone since the count variable now decreases from 20 to 0 Note: 8 instructions inside loop but ld count immediately followed by ld sum; unnecessary

  31. Loop With Jump to Test Rewrite: start: ja test loop: add sum st sum ld count sub @1 st count test: ld count jnz loop done: ldc msg sout ld sum dout ldc '\n' aout halt end start ; data count: dw 20 msg: dw “sum = “ sum: dw 0 @1 dw 1 Original Problem: Add the first twenty positive integers Programming Logic Problem: Gone since the count variable now decreases from 20 to 0 Note: 7 instructions inside loop ad ld count immediately followed by add sum; more efficient

  32. Loop With Jump to Test Rewrite: ld count start: ja test loop: add sum st sum ld count sub @1 st count test: jnz loop done: ldc msg sout ld sum dout ldc '\n' aout halt end start ; data count: dw 20 msg: dw “sum = “ sum: dw 0 @1 dw 1 Original Problem: Add the first twenty positive integers Programming Logic Problem: Gone since the count variable now decreases from 20 to 0 Note: 6 instructions inside loop ad ac == count immediately followed by add sum; more efficient

  33. Accesses the memory location pointed to by the ac register. Used to dereference pointers. Indirect instruction

  34. ac == address ac == value NOTE: ldi only needs one thing to start – address – and then it gets the value. However, sti is mem --> mem so needs two addresses to start but only has one register (ac) pops the stack automatically

  35. ldi and sti instructions • ldi loads the ac register from the memory location pointed to by the ac register. • sti pops the stack and stores the popped value at the memory location pointed to by the ac register. • So we use the stack to store the second address

  36. ld p ; p contains an address ldi st x Assembler code forx = *p; In C or C++, *p means “the value pointed at by p” and we say we are “dereferencing p” In Java, if p is a “reference” then a “value pointed at by p” might be p.x.

  37. x = *p

  38. Java Example: class P { public int : x,y}; int z; ... P p = newP(); ... z = p.y; • Assembler code for z = p.y; @2 dw 2 ld p add @2 ; integer is 2 bytes ldi st z