Trying to understand this short assembler instruction, but I don't understand

We had a task, this was an assembler instruction of a 2-addressing machine:

mov 202, 100[r1+] 

Write down the minimum sequence of assembler instructions that replaces these instructions (see above)

Where

  • n[rx+] : the register is indexed in incremental increments; n is the index value, and rx is the register x

  • one numerical value: address / address

The addresses we must use are:

  • rx - register direct addressing
  • [rx] - register indirect addressing
  • #n - direct call

And we are allowed to use add, sub, mov.

I especially don’t understand what # means and why we need a substrate, well, actually, I don’t understand anything ... The problem is solved, therefore I am not looking for a solution. I need an explanation of how this is done, trying to figure it out myself, but it did not work. I hope that someone can and will help me, it will be so kind of you!

Decision:

 add #100, r1 mov #202, r2 mov[r2],[r1] sub #99, r1 
+5
source share
3 answers

Understand that assembly language is not a universal, standardized thing. It is officially defined by the program that reads it, which is an assembler. So no matter what this code defines as the language for this assembler, it is a language. Machine code is what is important to the processor, so you can have as many different assemblers and assembly languages ​​as you have users, as long as they all produce the correct machine code. Therefore, if you expect this to be universal, one rule applies to all types of things that do not apply.

You have provided enough information, although for experienced people, to see the habits of other assembler languages, reflected here.

 mov 202, 100[r1+] 

Thus, it seems that you are moving what is indicated in the addressing address 202, as you stated. to the place at r1 + 100, registered by the index, and incremental increment.

To replace this with a lot of instructions, since one line is the simplest as the lines of code go (how complicated and the number of hours is not necessary).

  • So you need to read the contents of address 202,
  • you need to temporarily add 100 to r1,
  • you need to write the contents of address 202 to address r1 + 100,
  • and then you need to take r1 and increase it (not r1 + 100, but r1 without an index).

The resulting solution does pretty much what:

 add #100, r1 mov #202, r2 mov[r2],[r1] sub #99, r1 

It adds 100 to r1, which we need to do temporarily (and will have to cancel later, because r1 is now not so in the long run). Then, since they restrict addressing modes, you need the register to hold address 202, so the value 202 is loaded into r2 just as the value 100 was added to r1. #number means just using this number.

Now you are allowed to use [rn] so that this step reads what is on address r2 (address 202), and writes it to address r1 (source r1 plus 100). Finally, because we want r1 to ultimately be the original plus 1, but we made it original plus 100, we need to subtract 99 r1 + 100-99 = r1 + 1


In C, it would be something like this:

 unsigned *r1; unsigned *r2; //r1 is set somewhere in here to something that is not defined in your question or problem. r1 += 100; //add #100,r1 r2 = 202; //mov #202,r2 *r1 = *r2; //mov [r2],[r1] r1 -= 99; //sub #99,r1 

with the assignment on the right and the operand on the left is not intuitive, since we basically write and code the result on the left and the operands on the right.

We do not code 202 = r2; , and instead write r2 = 202; , therefore mov r2,#202 more intuitive, but again the assembler language is determined by the people who wrote the assembler, and some people like it left to the right, and others to the left.

+6
source

In mov 202, 100[r1+] :

  • mov command
  • The source operand is memory at address 202 . In some assembly languages, you can also write [202] for clarity. There is no number # on the number, so it is an absolute addressing mode, not an immediate one.
  • The destination operand is memory at address r1 + 100 .
  • A side effect of addressing mode is r1++

Like many varieties of assembly language, this one apparently uses the # prefixes to indicate immediate constants. This is similar to the syntax of GNU / AT & T x86, where add $4, %eax adds 4, but add 4, %eax loads the value from absolute address 4 .


Your equivalent, obtained by your professor, adds 100 to r1 , and then subtracts 99 , since he wants to avoid offsets and post-increments in addressing modes. It is also clobbers r2 , which the single-instruction version does not, so it is not a replacement.

+3
source

Your decision is not displayed at all on input. At your entrance, R1 is in an unknown state. In our “solution”, R1 ends in a known state (1).

In addition, you need to know what the format of your nation is.

 mov 202, 100[r1+] 

looks like

 1) add 100 to the contents of R1 to create an address. 2) move 202 into that address 3) Add 2 to r1 (assuming a 2-byte machine word) 
-2
source

All Articles