770 likes | 904 Vues
Getting rid of the drudgery. Programming Fundamentals 5 Feliks Klu ź niak. Getting rid of the drudgery. Programming Fundamentals 6 Feliks Klu ź niak. Executive summary :
E N D
Getting rid of the drudgery Programming Fundamentals 5 Feliks Kluźniak Getting rid of the drudgery
Getting rid of the drudgery Programming Fundamentals 6 Feliks Kluźniak Getting rid of the drudgery
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Assembly time: source program (in assembly language) object program (in machine language) computer executing the assembler Getting rid of the drudgery
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
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
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
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