填写“n”汇编器中的内存位置

发布于 2025-01-13 23:20:42 字数 91 浏览 0 评论 0原文

我正在努力完成这项工作,但我做不到。任何人都可以帮助我并解释如何进行分配:“用值-1填充地址中的n个内存位置” // R1 <- n // R2 <- 地址

I'm trying to do the job, but I can't. Can anyone help me and explain how to make the assignment: "fill n memory locations from address with value -1" // R1 <- n
// R2 <- addr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2025-01-20 23:20:42

任何人都可以帮助我并解释如何完成作业:

这个想法是将问题分开并解决各个部分,然后将这些部分组合成一个整体解决方案。

排名不分先后:


一种用于设置内存的算法,用 C 编写:

int R1;
int *R2;
do {
    *R2++ = -1;
} while ( --R1 >= 0 );

您不必用汇编语言思考来想出算法。用你所知道的语言来完成,然后将其中的一部分进行汇编要容易得多。


您将需要进行内存写入。

内存写入是通过执行 M= 指令来执行的,例如 M=D。该操作将执行Memory[A]=D - 因此您需要A寄存器中的内存地址和要存储在D中的值在执行此操作之前先注册。


您被告知将值放在 R2 引用的位置,因此获取 R2 中变量的值并将其放入 A > 注册。 R2 是数据内存位置 2 的别名,因此这里,R2 是一个位于内存中的指针变量。

@R2     // put 2 in `A`
A=M     // fetch from Memory[A] and put the result back into A

系统告诉您使用 -1 来填充内存,因此请将 -1 放入 D 中。我们可以直接在 C 指令中执行此操作,因此不需要 @ 指令来加载 -1:

D=-1     // set D to -1

通过上述设置,您可以执行 M=D 用-1 填充一个内存位置。


要存储在连续的位置,您需要增加 R2 中的指针。


您将需要一个计数循环。

由于其他寄存器(AD)将忙于在循环内执行操作,因此计数器必须位于内存中的某个位置。并且您被告知计数器位于 R1 中 — 与 Memory[1] 相同。

您可以使用简单的序列递减计数器,如下所示:

@R1      // put 1, the memory address of R1, in A
M=M-1    // instruction the machine to decrement memory

但是,如果您还想测试计数器,请将计数器放入 D 寄存器并将其存储回内存并添加向后条件分支继续循环:

(loop1)   // define top of loop
...       // body of loop
@R1       // target the counter (A=&counter)
MD=M-1    // compute counter-1, store back to memory &counter, and keep copy in D
@loop1    // target the loop top (A=&loop)
D;JGE     // branch to repeat the loop if D >= 0

这将完成,例如:

do {
    ...
} while ( --counter >= 0 )

Can anyone help me and explain how to make the assignment:

The idea is to pick apart the problem and solve pieces, then put the pieces together into an overall solution.

In no particular order:


An algorithm for setting memory, in C:

int R1;
int *R2;
do {
    *R2++ = -1;
} while ( --R1 >= 0 );

You don't have to think in assembly language to come up with an algorithm.  Much easier to do in a language you know then take pieces of that into assembly.


You will need to do memory writes.

Memory writes are performed by doing an M= instruction, such as M=D.  That operation will do Memory[A]=D — so you need a memory address in the A register and a value to store in the D register before performing this operation.


You're told to put the values at the location referred to by R2, so fetch the value of variable in R2 and put it into the A register.  R2 is an alias for data memory location 2, so here, R2 is a pointer variable that lives in memory.

@R2     // put 2 in `A`
A=M     // fetch from Memory[A] and put the result back into A

You're told to use -1 to fill the memory, so put a -1 in D.  We can do this directly in a C-instruction, so don't need an @ instruction to load the -1:

D=-1     // set D to -1

With the above setup, you can do an M=D to fill one memory location with -1.


To store at successive locations, you'll need to increment the pointer in R2.


You will need a counted loop.

Since the other registers (A, D) will be busy doing things inside the loop the counter will have to be located somewhere in memory.  And you're told that the counter is in R1 — the same as Memory[1].

You can decrement the counter with a simple sequence as follows:

@R1      // put 1, the memory address of R1, in A
M=M-1    // instruction the machine to decrement memory

However, if you want to also want to also test the counter, bring the counter into the D register as well as storing it back to memory and add a backward conditional branch to continue the loop:

(loop1)   // define top of loop
...       // body of loop
@R1       // target the counter (A=&counter)
MD=M-1    // compute counter-1, store back to memory &counter, and keep copy in D
@loop1    // target the loop top (A=&loop)
D;JGE     // branch to repeat the loop if D >= 0

This will accomplish, for example:

do {
    ...
} while ( --counter >= 0 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文