Precedence and other issues
Hi Professor Ford,
I am your student in course comp238, and it seems to be a stupid question. but still I want to make clear about what I thought about the "precedence of logical operators".
In class, you mentioned that operator "and" and operator "or" have same precedence. And at same time you said that if no parentheses are added there will be ambiguity. In this sense, I guess there is no sense to define any two operator with same precedence which also will give ambiguity for operating sequence. In one word, "and" and "or" operators are just not SAME precedence at all.
I checked with text book, on page no. 10, it is said "Another general rule of precednece is that the conjunction operator takes precedence over the disjunction operator, so that ....". Also on table 7 at same page "Precedence of Logical Operators" clearly listed that "not" operator has 1 precedence, "and" has 2 precendence, "or" has 3 precendence, "conditional" operator has 4 precendence, "biconditional" has 5 precendence.
One more evidence is that when I program with C++, the logical operator "&&" which is "and" is always taking first precedence over "||" , "or " operator.
Hope it does bother you.
¡¡
Best regards,
¡¡
QingZhe Huang
One's Complement and Two's Complement
The section U of course COMP228 is like hell! The instructor is a chinese woman who is continually nervous, shy, avoiding to face questions proposed by students, lack of confidence of what she was trying to express. In one word, she is unqualified.
1's complement and 2's complement is rather a typical illustration of evolution of computer architechure which demonstrated the practicalism of human toword computer science. Computer from very beginning is just a tool for human and people are just want to use it with convenience. So, in this sense, almost all aspect of computer design is from the purpose of convenience and efficience. There is no nonsense empty-talk involved which is a common phenomenon in many humanic "science", if we can call them sciences. 1's complement and 2's complement is only the choice of no-choice. I often say that in computer science, it always seems that Nature-Creator leaves only one choice which means we have no other choice. Only in this sense, we can say that "no choice is our best choice". So is the situation of 1's and 2's complement.
I originally heard this in York university in Toronto. I still remember the professor vividly demonstrated step by step that it is our only solution to problem of representing of "signed numbers". However, in Concordia, this woman instructor made nobody understand what it really means.
Just imagine that one byte can have 256 unique combinations of bits which can represent number of 0-255. But for "signed numbers", we need to reserve one bit for "signs". And it is for our convenience we use MSB(most significant bit) to represent signs. It is only by convention that we use the left-most(logically) bit to represent minus number when it is 1 and 0 for positive numbers. Therefore within one byte we are left with 7bits to represent numbers apart from one sign number.
(Signed Number)
Sign number (MSB) | combination of other 7 bits | Range of number |
1 | 128 | 0000,0000 ---0111,1111 (0 to 127) |
0 | 128 | 1000,0000 ---1111,1111 (0 to -127) |
This is pretty straight forward to represent signed number. However, it is quite useless. As we have to implement two algorithm for minus and plus operation.
So, in order to simplify the two operation into one operation, like 5-3 = 5+(-3), which always use plus operation. We introduce the 1's complement, that is flip all number to make its minus couterpart.
(1's complement)
Sign number (MSB) | combination of other 7 bits | Range of number |
1 | 128 | 0000,0000 ---0111,1111 (0 to 127) |
0 | 128 | 1111,1111 --- 1000,0000 (0 to -127) |
You can see that 1's complement is just change our usual concept for minus number---"the absolute value bigger, the value is smaller." Actually it is opposite so that minus operation becomes plus operation.
But it still has the problem of "TWO" zeros: 0000,0000 for +0 and 1111,1111 for -0. Then we have a solution with 2's complement that after we flip, we add 1 to "move" all minus number by one to delete the annoying -0.
Sign number (MSB) | combination of other 7 bits | Range of number |
1 | 128 | 0000,0000 ---0111,1111 (0 to 127) |
0 | 128 | 1111,1111 --- 1000,0000 (-1 to -128) |
We can notice that the difference is that 1111,1111 is becoming -1 instead of -0. So, all is perfect.
Reply from Dr. Ford by E-mail
You are right! The "and" operator has precedence over "or" and the "conditional" operator has precedence over "biconditional". I will clear this up in class tomorrow. -- FORD
About Context Switch and DMA
It starts here...
Dear Sir, Regarding I/O, I have some doubts: 1. At what point of time, does the context switching happen? Before CPU sent INTA or just before transfer control to interrupt-service-routine? ¡¡ |
Context switching is done before the "main body" of the interrupt service
routine. It is done after INTA. Take note that INTA (interrupt acknowledge) is
sent by the CPU to determine which device needs attention, and the address of
the interrupt service routine for that device. Before executing the interrupt
service routine, context switching must be implemented so that the CPU could go
back to the interrupted work properly after doing the interrupt service routine.
¡¡
¡¡ | ¡¡ | 2. Suppose DMA starts transfer data, can CPU use data bus and
address bus during the whole process of DMA copying data? Or in other words,
is it true that DMA is using data bus and address bus continually during
transfering data? ¡¡ |
The CPU is always the master of the hardware system whether a DMA is being
executed or not. To access the memory, for example, CPU takes control. Only when
the CPU does not use the memory that the DMA controller takes over -- you call
this stealing memory cycles from the CPU.
Concerning the buses, concurrency is implemented -- if there are 3 buses, for
example, 3 events could happen at the same time. There are instances that the
CPU wouldn't need one or two or all the 3 of these buses, the DMA controller
"steals" them. In other words, CPU activity and DMA data transfer are happening
at the same time, but the buses are not assigned to DMA controller to the
duration of the transfer. In fact, DMA priority is low that it is just
"stealing" resources to do its task.
And it continues...
|
MoreOver...
DMA
Cycle stealing means that the DMA controller releases the bus
after each byte is transferred. Transferring 512 bytes would
therefore require that the DMA controller request the CPU bus (by
asserting HRQ) 512 times. Each byte transferred causes DREQn* to
be asserted so DREQn* will also be asserted 512 times. Since
data is being transferred from the peripheral to memory, IOR*
will be asserted.
Why
should we use literals?
Hi Mr. Nick Huang,
FAQ:
1. Since assembler can automatically detect whether format 4 of instruction should be used or not, why should
we design to allow programmer to explicitly show in assembly code that "format 4" is used by adding "+" at
beginning of code?
It gives programmer more flexibility to control.
2. Since assembly directive "Base ASymbol" informs assembler how to calculate base relative, then why should
we still need write "LDB ASymbol" to actually load "ASymbol" address into "base" register? Why don't we allow
assembler generates code automatically?
It simply gives programmer more flexibility and also more responsibility to take care.
¡¡
1. Problem 8, page 112
In our SIC/XE model, the ¡°SYMTAB¡± does not store the references of an operand. So, we need to add one column in ¡°SYMTAB¡± as ¡°References¡± which is somewhat a linked list.
Symbol |
Value |
Flag |
References |
Alpha |
1003, 1009 |
Error |
1000, 1003 |
In the ¡°pass 2¡± when operands are checked, also adds the referred location counter number to ¡°SYMTAB¡± at column ¡°References¡±. Then when error message is output, we can also output the referred lines or location counter number of the duplicated-declared operands.
¡ïThe following part is suggested by Mr. Fang.
¡ïOne more point here, the "value" column should also be a linked list to store all "duplicated defined" symbols. So, even in "pass 1", when duplicated defined symbols are encountered, not only the error flag should be written into "SYMTAB" for the symbol, but also the address should be stored in value column so that these "labels" are also able to be printed out. So, the value field should also be a linked list instead of a single address.¡ï
2. Problem 12, page 116
The problem for assembler is that for the same mnemonic code like ¡°ADD¡±, different object code needs to be generated according to its operand. In other words, mnemonic code is not one-to-one with machine code. As for a machine that the length of instruction is not fixed, the only possible way to differentiate instructions is by its unique OPCODE. Since the operands are different, the OPCODE must be different either. Otherwise processor is unable to decode instruction correctly. Therefore assembler need to do some extra job to check the type of operand to decide which OPCODE should be used accordingly. Taking ¡°MASM¡± as an example, the same mnemonic ¡°ADD¡± will have different OPCODE according to different operands.
To solve this problem, during ¡°pass 2¡± the OPCODE will not be generated until the type of operand has been determined by checking the operand in ¡°SYMTAB¡±. And accordingly in ¡°SYMTAB¡± we need to add one more column for symbols----¡°Type¡±. So, when assembler parsed the definition of a symbol, it needs to store its ¡°type¡± in ¡°SYMTAB¡± for future reference.
3. Problem 8, page 118
a. LDA #3
This is using immediate addressing mode, that is the target address is ¡°0003¡±. And its object code should be ¡°012003¡±.
b. LDA #THREE
This is also using immediate addressing mode, however, the operand is a symbol ¡°THREE¡±. The target address should be the value of this symbol in ¡°SYMTAB¡±. Usually the value of a symbol in ¡°SYMTAB¡± is its address. But the assembly directive
¡°THREE EQU 3¡± will let assembler to store value ¡°3¡± into ¡°SYMTAB¡± instead of its address which is the value in ¡°location counter¡±. So, the target address becomes 003. And if we assume PC relative mode is used, the object code should be ¡°012003¡±. It is same as a.
c. LDA THREE
This code is meaningless! As in ¡°SYMTAB¡± the value of symbol ¡°THREE¡± is 3 which is not the address of this symbol by ¡°EQU¡± directive. And immediate addressing mode is not indicated. During execution time, processor will try to fetch value at memory address of ¡°003¡± which is incorrect!
4. Problem 24, page 121
The reason of RDREC-COPY is an invalid expression is because ¡°COPY¡± is not a symbol stored in ¡°SYMTAB¡±. So, the obvious solution is to add file name---¡°COPY¡± into ¡°SYMTAB¡± which has the same value or address as ¡°FIRST¡± or the first label in program.
The reason of RDREC-COPY is an invalid expression is because ¡°COPY¡± and "RDREC" are not available at assembly time. They are only known at loading time by the loader, so, for assembler it should provide enough information for loader to modify the address at loading time when the exact address of ¡°COPY¡± and "RDREC" are known. This can be done by adding modification records in object file. Suppose this expression appears in "COPY" block, then "RDREC" would be like a "EXTREF" which need to be modified in loading time.
5. Problem 5, page 122
My solution is to write whatever the immediate value or defined operand first into the operand part of object code and later add the undefined symbol value when its definition is met.
Symbol |
Value |
¡ |
¡ |
ENDFIL |
* |
¡ |
¡ |
|
|
|
|
So, the instruction ¡°JEQ ENDFIL+3¡± would be interpreted first as 30 0003, please note that the value ¡°+3¡± is write at the operand part and undefined symbol ¡°ENDFIL¡± is entered SYMTAB. When definition of ¡°ENDFIL¡± is later encountered, its address, say 2024, will be ¡°ADD¡± to the operand part. That is 2024 + 0003 = 2027. So, forward reference is not replacing the operand part, instead ¡°CALCULATED¡± at operand part.
Further to the question, say we want to handle minus sign of forward reference like:
¡°JEQ 3-ENDFIL¡± where operand may have different sign here. My solution is to add one sign column in reference linked list in SYMTAB as following:
Symbol |
Value |
¡ |
¡ |
ENDFIL |
* |
¡ |
¡ |
|
|
|
|
So, when updating forward reference according reference link list, assembler need to check the sign column to decide whether the value should be ¡°POSITIVE¡± OR ¡°NEGATIVE¡±. Say the ENDFIL address is 2024, now the operand should be 0003 + (-2024) = -2021.
¡¡