Some Experiments with the MARIE This lecture focuses on understanding the MARIE assembly language and the emulator used to run that assembler. Remember that the emulator being described here is a modification of the original MARIE emulator. We begin with examination of the immediate instructions. There are three instructions of interest. Clear Opcode = 0x0A Clears the accumulator. This is not an immediate instruction, but acts like one. AddM Opcode = 0x0E Add immediate to the accumulator SubM Subtract immediate from the accumulator. Opcode = 0x0F Format of the AddM and SubM Every MARIE instruction fills a 16–bit word. The format of every instruction is the same. Bit Field 15 12 4–bit opcode 11 0 Address, if used. For the AddM and SubM instructions, the field called “Address” actually contains a value to be added or subtracted. AddM 4 Adds 4 to the value stored in the accumulator SubM 6 Subtracts 6 from the value stored in the accumulator. There are a number of questions I propose to ask and answer, using the MARIE emulator: 1. Are these immediate values hexadecimal (probably). 2. Are leading zeroes required? Should “6” be written as “006”? 3. Can negative values be placed in the address field (probably not)? Review of Sign Extension The range of values stored using 16–bit two’s–complement arithmetic is – (215) through (215) – 1 inclusive, or –32,768 through 32,767. The range of values stored using 12–bit two’s–complement arithmetic is – (211) through (211) – 1 inclusive, or –2,048 through 2,047. The range of values stored using 12–bit unsigned arithmetic is 0 through (212) – 1 inclusive, or 0 through 4,095. How is the 12–bit immediate operand handled? Some of the steps may appear obvious. 1. The 12–bit value is expanded in some way to a 16–bit value. 2. That 16–bit value is then added to, or subtracted from, the value in the accumulator and stored into the accumulator. Question: What is the nature of this expansion? Sample 12–bit Numbers Consider the decimal number 13. As a 12–bit number, this would be stored as either 0000 0000 1101, or 0x00D. It would be expanded to 16–bit representation by just adding leading zeroes. As a 16–bit number, we would have 0000 0000 0000 1101, or 0x000D. What about negative 13? Producing this as a 12–bit number, we have: 12–bit binary for +13 0000 0000 1101 The one’s complement 1111 1111 0010 12–bit binary for –13 1111 1111 0011, or 0xFF3. Expanding the Negative Number to 16 bits Expanding this to 16 bits requires preservation of the sign bit. Expanding the value to 0x0FF3 would yield the value +4,083. 1111 1111 0011, or 0xFF3 The 12–bit number: Incorrectly expanded: 0000 1111 1111 0011, or 0x0FF3. This is a positive number with value 15256 + 1516 + 3 = 4,083 To preserve the sign, the value is expanded to the 16–bit value 1111 1111 0011, or 0xFF3 The 12–bit number: Correctly expanded: 1111 1111 1111 0011, or 0xFFF3. This is a 16–bit negative number. Sample 1: The Immediate Values are Hexadecimal Here is the sample code for assembly. It has only three instructions. Clear AddM 012 Halt Here is the assembly listing of this program. The opcodes are in red. Assembly listing for: Sample01.max Assembled: Tue Sep 28 11:51:37 EDT 2010 000 A000 | CLEAR 001 E012 | ADDM 012 002 0000 | HALT For a value of decimal 12, the object code would have been E00C. Sample 2: Are Leading Zeroes Required? Change the above code to the following. Clear AddM 12 Halt Here is the assembly listing of this program. Assembly listing for: Sample02.max Assembled: Tue Sep 28 12:04:56 EDT 2010 000 A000 | CLEAR 001 E012 | ADDM 012 002 0000 | HALT Apparently, the leading zero is supplied by the assembler. This suggests that the number is built by reading right to left. Sample 3: Does the Mode Handle Signed Numbers? Consider the 12–bit value 0x9F3. As a 12–bit number, we might have the following: In other words, does this expand to a 16–value as 1) An unsigned number, to become or 2) A 16–bit sign extended number (Run Sample04B.MAX) 0000 1001 1111 0011 1111 1001 1111 0011 Sample 4: Does the Mode Handle Signed Numbers? Here are some 12–bit values: +18 is stored as 0x012 –13 is stored as 0xFF3. Here is the code for the next sample. Clear AddM 012 AddM FF3 Halt If the number FF3 is treated as unsigned, the sum will be 0x1005. If the number FF3 is treated as signed, the sum would be 0x0005. (Sample04.Max) More on Sample 4 Here is what happened in the assembly process for the above. Assembly listing for: Sample04.max Assembled: Wed Sep 29 20:40:48 EDT 2010 000 A000 | CLEAR 001 E012 | ADDM 012 002 E??? | ADDM FF3 **** Operand undefined. 003 0000 | HALT 1 error found. Assembly unsuccessful. What is the problem here? (SAMPLE04.LST) Sample 4: The Corrected Version Here is the correct version. Clear AddM 012 AddM 0FF3 //Notice the four hex digits. Halt Here is the assembled version Assembly listing for: Sample04A.max Assembled: Wed Sep 29 20:46:09 EDT 2010 000 A000 | CLEAR 001 E012 | ADDM 012 002 EFF3 | ADDM FF3 003 0000 | HALT Assembly successful. (SAMPLE04A.MAX) Sample 5: Z = X + Y Here is sample code that implements the above statement. Load X Add Y Store Z Halt X, HEX 000C Y, HEX FFF3 Z, HEX 0000 At the end of this, the address associated with label Z has the sum placed into it. Sample 5: Assembly Listing Assembly listing for: Sample05.max Assembled: Mon Oct 04 18:55:27 EDT 2010 000 1004 | LOAD X 001 3005 | ADD Y 002 2006 | STORE Z 003 0000 | HALT 004 000C | X HEX 000C 005 FFF3 | Y HEX FFF3 006 0000 | Z HEX 0000 Assembly successful. SYMBOL TABLE Symbol | Defined | References --------+---------+---------------------X | 004 | 000 Y | 005 | 001 Z | 006 | 002 ---------------------------------------