1 / 77

Getting rid of the drudgery

Getting rid of the drudgery. Programming Fundamentals 5 Feliks Klu ź niak. Getting rid of the drudgery. Programming Fundamentals 6 Feliks Klu ź niak. Executive summary :

signa
Download Presentation

Getting rid of the drudgery

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. Getting rid of the drudgery Programming Fundamentals 5 Feliks Kluźniak Getting rid of the drudgery

  2. Getting rid of the drudgery Programming Fundamentals 6 Feliks Kluźniak Getting rid of the drudgery

  3. Executive summary: • We learn the principles of assembly language: a notation that allows us to write machine-language programs in a more convenient way. Getting rid of the drudgery

  4. Recall our program that adds two integers: address contents #0000: #20004 #0001: #80005 #0002: #10006 #0003: #00000 #0004: #00003 #0005: #00004 #0006: #00000 Getting rid of the drudgery

  5. Recall our program that adds two integers: address contents This is actually: #0000: #20004 0010000000000100 #0001: #80005 1000000000000101 #0002: #10006 0001000000000110 #0003: #00000 0000000000000000 #0004: #00003 0000000000000011 #0005: #00004 0000000000000100 #0006: #00000 0000000000000000 Getting rid of the drudgery

  6. Recall our program that adds two integers: address contents This is actually: #0000: #20004 0010000000000100 #0001: #80005 1000000000000101 #0002: #10006 0001000000000110 #0003: #00000 0000000000000000 #0004: #00003 0000000000000011 #0005: #00004 0000000000000100 #0006: #00000 0000000000000000 Can you see why we don’t want to read and write such stuff directly?  Getting rid of the drudgery

  7. First, we would like to use a mnemonic form for the opcodes: address contents #0000: #20004 LOA#0004 #0001: #80005 ADD #0005 #0002: #10006 STO #0006 #0003: #00000 HLT #0000 #0004: #00003 LIT#00003 #0005: #00004 LIT #00004 #0006: #00000 LIT #00000 NOTE: LIT is not really an opcode of a machine instruction. It is a pseudo-instruction that tells us the “operand” is a data item Getting rid of the drudgery

  8. Second, we don’t want to be forced to write the leading zeroes: address contents #0000: #20004 LOA#4 #0001: #80005 ADD #5 #0002: #10006 STO #6 #0003: #00000 HLT #0 #0004: #00003 LIT#3 #0005: #00004 LIT#4 #0006: #00000 LIT #0 Getting rid of the drudgery

  9. Third, addresses in hexadecimal are OK, but data items would sometimes be more convenient in decimal: address contents #0000: #20004 LOA #4 #0001: #80005 ADD #5 #0002: #10006 STO #6 #0003: #00000 HLT #0 #0004: #00003 LIT3 #0005: #00004 LIT4 #0006: #00000 LIT 0 Getting rid of the drudgery

  10. Fourth, we most certainly want to be able to write comments (this is more important than you might think!): address contents #0000: #20004 LOA #4 ; load the first data item #0001: #80005 ADD #5 ; add the second data item #0002: #10006 STO #6 ; store the result #0003: #00000 HLT #0 ; halt #0004: #00003 LIT3 ; the first data item #0005: #00004 LIT 4 ; the second data item #0006: #00000 LIT 0 ; space for the result Notice that a comment begins with a free-standing semicolon and extends to the end of the line. Getting rid of the drudgery

  11. Fourth, we most certainly want to be able to write comments (this is more important than you might think!): address contents #0000: #20004 LOA #4 ; load the first data item #0001: #80005 ADD #5 ; add the second data item #0002: #10006 STO #6 ; store the result #0003: #00000 HLT #0 ; halt #0004: #00003 LIT3 ; the first data item #0005: #00004 LIT 4 ; the second data item #0006: #00000 LIT 0 ; space for the result Notice that a comment begins with a free-standing semicolon and extends to the end of the line. Free-standing: not part of a character literal (’;’) or a string literal (’’alpha;bet’’). Getting rid of the drudgery

  12. What if we wanted to add a third data item? address contents #0000: #20004 LOA #4 ; load the first data item #0001: #80005 ADD #5 ; add the second data item #0002: #10006 STO #6 ; store the result #0003: #00000 HLT #0 ; halt #0004: #00003 LIT 3 ; the first data item #0005: #00004 LIT 4 ; the second data item #0006: #00000 LIT 0 ; space for the result Getting rid of the drudgery

  13. As we relocate some parts of the program, we must modify addresses that refer to them! address contents #0000: #20005LOA #5 ; load the first data item #0001: #80006 ADD #6 ; add the second data item #0002: #80007 ADD #7 ; add the third data item #0003: #10008 STO #8 ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 LIT3 ; the first data item #0006: #00004 LIT 4 ; the second data item #0007: #00011 LIT 17 ; the third data item #0008: #00000 LIT 0 ; space for the result Getting rid of the drudgery

  14. Fifth, we want to use symbolic labels: address contents #0000: #20004 LOA A ; load the first data item #0001: #80005 ADD B ; add the second data item #0002: #10006 STO RESULT ; store the result #0003: #00000 HLT #0 ; halt #0004: #00003 A LIT 3 ; the first data item #0005: #00004 B LIT 4 ; the second data item #0006: #00000 RESULT LIT 0 ; space for the result NOTE: The label A should not be confused with the accumulator! Getting rid of the drudgery

  15. Fifth, we want to use symbolic labels: address contents #0000: #20004 LOA A ; load the first data item #0001: #80005 ADD B ; add the second data item #0002: #10006 STO RESULT ; store the result #0003: #00000 HLT #0 ; halt #0004: #00003 A LIT 3 ; the first data item #0005: #00004 B LIT 4 ; the second data item #0006: #00000 RESULT LIT 0 ; space for the result Each label represents an address: the values of labels A, B and RESULT are #4, #5 and #6, respectively. Getting rid of the drudgery

  16. As we relocate some parts of the program, we need no longer modify labels that refer to them! address contents #0000: #20005LOA A ; load the first data item #0001: #80006 ADD B ; add the second data item #0002: #80007 ADD C ; add the third data item #0003: #10008 STO RESULT ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 A LIT3 ; the first data item #0006: #00004 B LIT 4 ; the second data item #0007: #00011 C LIT 17 ; the third data item #0008: #00000 RESULT LIT 0 ; space for the result Getting rid of the drudgery

  17. As we relocate some parts of the program, we need no longer modify labels that refer to them! address contents #0000: #20005LOA A ; load the first data item #0001: #80006 ADD B ; add the second data item #0002: #80007 ADD C ; add the third data item #0003: #10008 STO RESULT ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 A LIT3 ; the first data item #0006: #00004 B LIT 4 ; the second data item #0007: #00011 C LIT 17 ; the third data item #0008: #00000 RESULT LIT 0 ; space for the result The values of some labels will change, but their names are the same. Getting rid of the drudgery

  18. As we relocate some parts of the program, we need no longer modify labels that refer to them! address contents #0000: #20005 LOA A ; load the first data item #0001: #80006 ADD B ; add the second data item #0002: #80007 ADD C ; add the third data item #0003: #10008 STO RESULT ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 A LIT 3 ; the first data item #0006: #00004 B LIT 4 ; the second data item #0007: #00011 C LIT 17 ; the third data item #0008: #00000 RESULT LIT 0 ; space for the result NOTE: The definition of a label must start in the first character of the line! Getting rid of the drudgery

  19. As we relocate some parts of the program, we need no longer modify labels that refer to them! address contents #0000: #20005 LOA A ; load the first data item #0001: #80006 ADD B ; add the second data item #0002: #80007 ADD C ; add the third data item #0003: #10008 STO RESULT ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 A LIT 3 ; the first data item #0006: #00004 B LIT 4 ; the second data item #0007: #00011 C LIT 17 ; the third data item #0008: #00000 RESULT LIT 0 ; space for the result NOTE: The definition of a label must start in the first character of the line! The mnemonic opcode must not start in the first character. Getting rid of the drudgery

  20. What would happen if we forgot to write the HLT instruction? LOA A ; load the first data item ADD B ; add the second data item ADD C ; add the third data item STO RESULT ; store the result HLT #0 ; halt A LIT 3 ; the first data item B LIT 4 ; the second data item C LIT 17 ; the third data item RESULT LIT 0 ; space for the result Getting rid of the drudgery

  21. What would happen if we forgot to write the HLT instruction? LOA A ; load the first data item ADD B ; add the second data item ADD C ; add the third data item STO RESULT ; store the result HLT #0 ; halt A LIT 3 ; the first data item B LIT 4 ; the second data item C LIT 17 ; the third data item RESULT LIT 0 ; space for the result Always make sure that your program does not begin to execute data! Getting rid of the drudgery

  22. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result Getting rid of the drudgery

  23. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. Getting rid of the drudgery

  24. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result 0: 1: 2: 3: 4: 5: 6: 7: 8: First, we count the words and assign values to the labels. Getting rid of the drudgery

  25. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result 0: 1: 2: 3: 4: 5: 6: 7: 8: First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Getting rid of the drudgery

  26. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result 0: 1: 2: 3: 4: 5: 6: 7: 8: First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  27. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  28. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  29. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  30. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  31. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  32. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  33. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  34. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  35. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result First, we count the words and assign values to the labels. We see that A = 5, B = 6, C = 7, RESULT = 8 . Next, we go over the program once again. For each word we look up the mnemonic, translate it to binary, translate the operand to binary and output the assembled binary word. Getting rid of the drudgery

  36. It is quite easy to translate from this form to binary code by hand. address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result Getting rid of the drudgery

  37. It is quite easy to translate from this form to binary code by hand. It is also boring, time-consuming, error-prone… address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result Getting rid of the drudgery

  38. Easy, boring, time-consuming, error-prone… In short, a perfect task for a computer! address contents #0000: #20005LOA A ; load the first data item #0001: #80006ADD B ; add the second data item #0002: #80007ADD C ; add the third data item #0003: #10008STO RESULT ; store the result #0004: #00000HLT #0 ; halt #0005: #00003A LIT 3 ; the first data item #0006: #00004B LIT 4 ; the second data item #0007: #00011C LIT 17 ; the third data item #0008: #00000RESULT LIT 0 ; space for the result Getting rid of the drudgery

  39. Easy, boring, time-consuming, error-prone… • In short, a perfect task for a computer! • address contents • #0000: #20005LOA A ; load the first data item • #0001: #80006ADD B ; add the second data item • #0002: #80007ADD C ; add the third data item • #0003: #10008STO RESULT ; store the result • #0004: #00000HLT #0 ; halt • #0005: #00003A LIT 3 ; the first data item • #0006: #00004B LIT 4 ; the second data item • #0007: #00011C LIT 17 ; the third data item • #0008: #00000RESULT LIT 0 ; space for the result • A program that performs such a translation is called an assembler. Getting rid of the drudgery

  40. easy, boring, time-consuming, error-prone… In short, a perfect task for a computer! address contents #0000: #20005 LOA A ; load the first data item #0001: #80006 ADD B ; add the second data item #0002: #80007 ADD C ; add the third data item #0003: #10008 STO RESULT ; store the result #0004: #00000 HLT #0 ; halt #0005: #00003 A LIT 3 ; the first data item #0006: #00004 B LIT 4 ; the second data item #0007: #00011 C LIT 17 ; the third data item #0008: #00000 RESULT LIT 0 ; space for the result A program that performs such a translation is called an assembler. The convenient notation that it translates is called assembly language. Getting rid of the drudgery

  41. First, we run the assembler. The assembler takes our assembly language program as its input, and produces a file with binary words as its output. It also produces a listing. Getting rid of the drudgery

  42. First, we run the assembler. The assembler takes our assembly language program as its input, and produces a file with binary words as its output. It also produces a listing. Historically, the input file was often on punched cards, the output file also consisted of punched cards. But either one might as well be on paper tape, magnetic tape, a file on a magnetic disk… Getting rid of the drudgery

  43. First, we run the assembler. The assembler takes our assembly language program as its input, and produces a file with binary words as its output. It also produces a listing. The binary file can later be loaded into the computer whenever needed, and “executed”. Getting rid of the drudgery

  44. First, we run the assembler. • The assembler takes our assembly language program as its input, and produces a file with binary words as its output. • It also produces a listing. • The binary file can later be loaded into the computer whenever needed, and “executed”. • So we have two distinct time periods: • assembly time: when the program is being translated Getting rid of the drudgery

  45. First, we run the assembler. • The assembler takes our assembly language program as its input, and produces a file with binary words as its output. • It also produces a listing. • The binary file can later be loaded into the computer whenever needed, and “executed”. • So we have two distinct time periods: • assembly time: when the program is being translated; • runtime: when the program is being run (executed). Getting rid of the drudgery

  46. Assembly time: source program (in assembly language) object program (in machine language) computer executing the assembler Getting rid of the drudgery

  47. Assembly time: source program (in assembly language) object program (in machine language) computer executing the assembler Load time: computer executing the loader program object program (in machine language) Getting rid of the drudgery

  48. Assembly time: source program (in assembly language) object program (in machine language) computer executing the assembler Load time: Runtime: computer executing the object program computer executing the loader program object program (in machine language) Getting rid of the drudgery

  49. Assembly time: (assembly time is runtime for the assembler program) source program (in assembly language) object program (in machine language) computer executing the assembler Load time: Runtime: computer executing the object program computer executing the loader program object program (in machine language) (load time is runtime for the loader program) Getting rid of the drudgery

  50. An assembler is an example of a translator, i.e., a program that translates from one “language” to another: in this case from assembly language to “machine language”. Getting rid of the drudgery

More Related