Меньше или равно 8004 (машинный код)

Я прохожу курс, и я застрял на одной из головоломок расширения. Он не отмечен и ни к чему не относится.

Я проведу вас через свои размышления. Любые (очень) тонкие намеки будут оценены, я все еще хотел бы понять это сам, но я зашел в тупик.

Вот вопрос.

«Напишите программу 8004, которая работает для любых двух чисел (значения 0..255), независимо от того, меньше или равно первое число второму.

Первое число будет храниться по адресу памяти 14, а второе по адресу 15.

Если первое число меньше или равно второму, выведите ненулевое значение. В противном случае, если это не так (т. е. больше), выведите 0».

У меня есть 16 байт, чтобы сделать это. Хорошо 14, потому что последние два байта занимают входные числа.

Вот полный набор инструкций:

Полный набор инструкций и визуальное оформление

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;
}

person JohnAwesome    schedule 29.08.2016    source источник


Ответы (1)


Да!! Я получил это наконец!

Ключевым моментом было посмотреть, как числа могут быть интерпретированы как двухбайтовые инструкции, если перейти к ним из другого места. Есть подсказка, если кто-то еще застрял. Посмотри на свои прыжки!

person JohnAwesome    schedule 29.08.2016