Successive loops in Assembly

Asked

Viewed 232 times

0

I am studying Assembly however I am very difficult, I have to solve an exercise but I did not understand what to do.

"Loop successive subtractions to negative. load, sub."

Can someone help me.

  • Which Assembly? x86? What operations allowed? What should I subtract from what?

  • You only need to indicate the memory positions and access with the code.

  • I will provide initial values, too.

1 answer

0


Using the MOV from the x86 instruction set you can recover a word (WORD, 2 bytes) memory from an address. We assume that in the address 0200 and 0201 the value for AX is stored, and 0202 and 0203 is stored the value for BX.

 072C:0100     MOV AX,[0200]
 072C:0103     MOV BX,[0202]

See that I incremented two positions because AX is a WORD with two bytes. You can initialize the addresses with the mov also, for example:

MOV WORD PTR [0200],0005
MOV WORD PTR [0202],0002

The result at the address 0200 will be:

inserir a descrição da imagem aqui

Being

0200 05
0201 00
0202 02
0203 00

Remember that the order is always reversed but while recovering the data with mov the data comes back correctly as:

byte byte    word

0201 0200 -> 0005
0203 0202 -> 0002

The following code is to perform the successive LOOP by subtracting both variables into AX e BX, being AX - BX, soon must BX < AX, continuing at the address 0107

 072C:0107     SUB AX,0001
 072C:010A     SUB BX,0001
 072C:010D     JNZ 107
 072C:010F     INT 20

While BX is non-zero, i.e., if the FLAG ZERO (indicating that the last operation resulted in zero) is false, the flow goes back to the address 0107 SUB AX.

Following is the final code for the program

inserir a descrição da imagem aqui

Ending with the interrupt call INT 20, to return control of the program to the operating system, and preventing the program from running trash.

Observing

Here I used SUB, but you could use DEC, since you only decrease only 1 always. And the instruction LOAD does not exist in x86, but does exist LODSB or LODSW, which are more complex, and are used for STRING operations (data array).

I’m one using the program of MS-DOS debug to debug. Also works with SYMDEB.EXE also of MS-DOS.

If you are going to use another high-level language like C, or pascal, you can use label: before the loop and JNZ @label instead of JNZ 0107.

  • The final code will be just this?

  • That, register allocation, decrement and loop.

  • Friend did not understand very well no.

  • You ran the program?

  • gave several mistakes, but I think I did wrong here.

  • Depending on the debugger it is not possible to access the RAM. And in others they use EAX instead of AX, and EBX instead of BX

  • No, buddy, the code is right. But according to my teacher, the IAS instructions, machine was.

  • The IAS is not very well known, it’s another processor. Generally, people teach x86 because it’s the processor instruction set that Intel has used since 1970. I’ll look at how the IAS works and I’ll answer you

  • https://en.wikipedia.org/wiki/Instruction_set_architecture. What program do you use to test this?

  • tested on gcc from Linux.

  • Oh yes, I suggest creating a new question, emphasizing that you are working with GCC and IAS. If you want you can create a question on how to convert x86 code to IAS, but I believe that since there is no IAS tag, you will rarely get an answer.

  • according to my teacher only 2 lines are wrong, but did not tell me which

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.