【发布时间】:2016-08-29 12:45:47
【问题描述】:
我正在学习一门课程,但遇到了一个扩展难题。它没有标记,不计入任何内容。
我会带你了解我的想法。任何(非常)微妙的提示将不胜感激,我仍然想自己弄清楚,但我已经走到了死胡同。
问题来了。
“编写一个 8004 程序,计算任意两个数字(值 0..255),无论第一个数字是否小于或等于第二个。
第一个数字将存储在内存地址 14 中,第二个数字将存储在地址 15 中。
如果第一个数字小于或等于第二个数字,则打印非零值。否则,如果不是(即更大),则打印 0。”
我有 16 个字节要做。好吧 14 个因为最后两个字节被输入数字占用了。
这是完整的指令集:
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;
}
【问题讨论】:
标签: machine-code