计算机入门 作业笔记(1)
Contents
Bonus Problem
resource: Glenn Brookshear_ Dennis Brylow-Computer Science - An Overview-Pearson Education Limited (2015) P134 T24
24. A game that used to be popular among computer hobbyists is core wars—a variation of battleship. (The term core originates from an early memory technology in which 0s and 1s were represented as magnetic fields in little rings of magnetic material. The rings were called cores.) The game is played between two opposing programs, each stored in different locations of the same computer’s memory. The computer is assumed to alternate between the two programs, executing an instruction from one followed by an instruction from the other. The goal of each program is to cause the other to malfunction by writing extraneous data on top of it; however, neither program knows the location of the other.
a. Write a program in the machine language of Appendix C that approaches the game in a defensive manner by being as small as possible.
b. Write a program in the language of Appendix C that tries to avoid any attacks from the
opposing program by moving to different locations. More precisely, beginning at location 00, write a program that will copy itself to location 70 and then jump to location 70.
c. Extend the program in (b) to continue relocating to new memory locations. In particular, make your program move to location 70, then to E0 (= 70 + 70) then to 60 (= 70 + 70 + 70) etc.
Appendix C
A Simple Machine Language
In this appendix we present a simple but representative machine language. We begin by explaining the architecture of the machine itself.
The Machine’s Architecture
The machine has 16 general-purpose registers numbered 0 through F (in hexadecimal). Each register is one byte (eight bits) long. For identifying registers within instructions, each register is assigned the unique four-bit pattern that represents its register number. Thus register 0 is identified by 0000 (hexadecimal 0), and register 4 is identified by 0100 (hexadecimal 4). There are 256 cells in the machine’s main memory. Each cell is assigned a unique address consisting of an integer in the range of 0 to 255. An address can therefore be represented by a pattern of eight bits ranging from 00000000 to 11111111 (or a hexadecimal value in the range of 00 to FF). Floating-point values are assumed to be stored in an eight-bit format discussed in Section 1.7 and summarized in Figure 1.24.
The Machine’s Language
Each machine instruction is two bytes long. The first 4 bits provide the op-code; the last 12 bits make up the operand field. The table that follows lists the instructions in hexadecimal notation together with a short description of each. The letters R, S, and T are used in place of hexadecimal digits in those fields representing a register identifier that varies depending on the particular application of the instruction. The letters X and Y are used in lieu of hexadecimal digits in variable fields not representing a register.
Blank space for thinking.
My Solution
题目大致要求你用他给的一个鬼畜cpu的指令集,完成一个自我复制的程序。
首先你会发现把一个位置的值放到另一个必须先存入寄存器,并且把寄存器的值存入内存需要在内存中提前确定将存入的位置。
一个朴素的想法是在寄存器中维护两个指针 $i$,$j$ 表示这个程序和复制过去的程序目前的位置。
复制一个值,只需先把 $i$ 先放到那个指令的操作数上,再执行这个指令。
然而你会发现你并不知道那个指令的具体位置!
考虑再复制时实现一个判断语句,如果当前执行的是偶数条指令的第二个字节,就加70。通过额外增加类似2F00的空语句可以完成本题。
My Answer
Main Memory 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 26 03 21 00 24 01 22 70 25 70 31 0D 13 00 2F 00 1 88 16 2F 00 20 03 B8 1C 2F 00 B0 1E 53 35 32 21 2 33 00 2F 00 51 14 2F 00 52 24 20 34 2F 00 B1 70 3 2F 00 B0 0A 00 00 00 00 00 00 00 00 00 00 00 00 4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 R0:00 R1:00 R2:00 R3:00 R4:00 R5:00 R6:00 R7:00 PC: 00 R8:00 R9:00 RA:00 RB:00 RC:00 RD:00 RE:00 RF:00 IR: 0000
Hint
此外,这个题的严格要求是JUMP到下一个程序位置时,两个程序完全相同,或者只有JUMP语句不同。可以使用其他思路,更好更妙。
No Comments