我正在上一门课,但我被其中一个扩展难题困扰了。它没有标记,也不会计入任何东西。
我会告诉你我的想法。任何(非常)微妙的暗示都将不胜感激,我仍然想自己弄清楚,但我已经走进了死胡同。
问题来了。
“编写一个8004程序,无论第一个数字是否小于或等于第二个数字,它都能计算出任意两个数字(值0..255)。
第一个数字将存储在存储器地址14中,而第二个数字将存储在地址15中。
如果第一个数字小于或等于第二个数字,则打印一些非零值。否则,如果不是(即大于0),则打印0。“
我有16个字节来做这个。因为最后两个字节被输入的数字占据了。
下面是完整的指令集:
Full instruction set and Visual
Here's my thinking:
1. Swap second number (addr 15) and R0. Second number now loaded into R0.
2. Swap first number and R0. Now First number is in R0, second number is in addr 14. We can keep swapping R0 and addr 14 to switch between testing the First and Second number.
3. Check if First number is 0.
4. If it is 0, the First number is less than or equal to second number -> print Non Zero.
5. If it is not 0, it is unknown, and -1 from the First number for next time.
6. SWAP First Number and Second Number
7. Check if Second number is 0.
8. If it is Not Zero, it's unknown, and -1 from Second number and goto 2. (SWAP again and check the first number..)
9. If it is 0, then the first number is greater than the second number -> print 0.
I'm fairly sure this algorithm works for all cases. BUT it won't FIT! I've tried most everything!
My most recent thinking is along the lines of:
14 15 - second number in R0
14 14 - swap first and second number, use memory address 14 as cache for first/second number
9 x - check if first number is zero, if it is zero, jump to print Not Zero (less than or equal)
2 - minus 1 from first number, so first number -1 will be checked next time
14 14 swap the number so second number is in R0
8 y - check if second number does not equal zero, if it doesn't , -1 and jump to "14 14" (line 2) to swap and check first number again
// don't know how I can -1 from R0 and jump
7 0 if it does equal zero, print 0 (greater than)
Can't minus 1 before checking as if it is 0, then it becomes 255 and messes up the comparison. Need to check for 0 first for both numbers. Need to check the first number first because if the second number -1 = 0 then it is is equal. So if I check second number first, then the first number -1 could equal zero meaning it is greater than or EQUAL to, which is not what we are looking for.
Don't really know where to go from here..
编写了一个C程序来测试算法-
#include <stdio.h>
#define LESS_THAN_OR_EQUAL 1
#define GREATER_THAN 0
int main (void) {
int result;
int firstNumber = 70;
int secondNumber = 50;
// starts here
jump:
if (firstNumber == 0) {
result = LESS_THAN_OR_EQUAL;
} else {
firstNumber--;
if (secondNumber != 0) {
secondNumber--;
goto jump;
} else {
result = GREATER_THAN;
}
}
printf("Result: %d", result);
return 0;
}
发布于 2016-08-29 19:23:14
是的!!我终于得到它了!
关键是看看如果从不同的位置跳转到数字,如何将数字重新解释为两个字节的指令。如果其他人被卡住了,就会有一个提示。看看你的跳跃!
https://stackoverflow.com/questions/39206704
复制