Loop of repetition in Assembly

Asked

Viewed 2,830 times

4

How can I do a repeat a certain number of times in Assembly (no MIPS)?

Example in pseudocode, assuming the variable registrador be a registrar that I want to receive all numbers from 1 to 6 (one at a time, obviously)

var i = 1;
while(i <= 6)
{
    registrador = i;
    i = i + 1;
}
  • What is this registrador?

  • It is to be a register of the processor, I want that at the end of the code it has the value 6 (as in the code presented). As I can run step by step, I can see the values being changed.

  • I don’t know anything about MIPS, but it would look something like this: https://godbolt.org/g/Nlcax8

  • Neither do I, @bigown But that code is terrible :p

  • The GCC’s fault, not mine :D

3 answers

3

I made a version a little simpler than the other answer. Without using the move - I changed my mind right after.

main:
    li $t0, 0             # $t0 é o incrementador (o "i" da pergunta)
    li $t1, 6             # $t1 é o valor máximo (serão 5 loops)

loop: 
    beq $t0, $t1, done    # se o $t0 for igual a $t1, vai para 'done' (acabou o loop)
    addi $t0, $t0, 1      # incrementar $t0 em 1
    j loop                # pular para (goto) 'while'

done:

2


Something like that?

Instruction reference of the MIPS.

.globl main

 main:
    li $v0, 0               # registrador
    li $t0, 1               # valor inicial do índice do laço

loop:
    bgt $t0, 5, exit        # se $t0 > 5, interrompa laço
    move $v0, $t0           # registrador = $t0
    addiu $t0, $t0, 1       # incrementa índice
    j loop                  # vá para o label [loop:]

exit:
  • What a horrible compiler GCC is for MIPS. I was even finding his Assembly very prolix.

  • @bigown to lessen frustration, remember: There are always worse. ;)

  • @jbueno referencia: https://en.wikipedia.org/wiki/MIPS_architecture#Jump_and_branch

0

program simq1; uses crt; var i,k:integer; vet:array [1.. 10] of integer;

Begin clrscr; writeln('What are the 10 numbers?'); for i:=1 to 10 Begin readln(vet[i]); end; for i:=1 to 10 Begin for k:=1 to 10 Begin if (i<>k) and (vet[i]=vet[k]) and (vet[k]<>-1) then Begin vet[k]:=-1; k:=10; end; end; end; writeln('The vector without repetition is'); for i:=1 to 10 Begin if vet[i]<>-1 then Begin writeln(vet[i]); end; end; readkey; end.

Browser other questions tagged

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