小于或等于8004(机器代码)

时间:2016-08-29 12:45:48

标签: machine-code

我正在做一个课程而且我被困在其中一个扩展难题上。它没有标记,也没有计入任何内容。

我带你了解我的想法。任何(非常)微妙的提示都会受到赞赏,我仍然希望自己弄清楚,但我已经走到了尽头。

这是问题所在。

"写一个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;
}

1 个答案:

答案 0 :(得分:0)

是!!我终于明白了!

关键是要看看如果从不同的位置跳转到两个字节指令,数字如何被重新解释。如果有其他人被卡住,那就是暗示。看看你的跳跃!

相关问题