# array – Create a code with machine instructions

## Question:

I have the following problem and I don't know where and how to start. The worst of all is that it is difficult for me to program with machine instructions:

There is an arrangement A = [a1; a2; …; an] of n positive integers with n an even integer.

I need to implement a code in IASSIM (von neumann architecture simulator) that from an array A, computes an array B defined as B = {| log2 a1 |, | log2 a2 |, …, | log2 an |} where | | is the floor function that rounds to the nearest smallest integer. It is recommended that I implement the | log2 x | and then see a way to loop through an array.

Attached a link with the simulator page and with the machine instructions that are used http://www.cs.colby.edu/djskrien/IASSim/ (the simulator is made in java that's why I put the label)

``````; calcular logaritmo base 2 de un número n el cual entregará la parte entera

loop:   S(x)->Ac+ n ;carga n al acumulador
Cc->S(x) log ;si el acumulador es >= 0 salta a log
halt            ; en otro caso termina
.empty

log:   S(x)->R resm ;copia el número 2 al AR
S(x)*R->A dos;se multiplica 2*2
At->S(x) resm ; se guarda en resm
S(x)->Ah+ uno ;se aumenta el contador
At->S(x) cont ;se guarda el contador
S(x)->Ac+ n ;se carga n al AC
S(x)->Ah-  uno;se decrementa n en 1
At->S(x)   n ;se guarda el n
Cu->S(x)   loop; vuelve al principio y hace el proceso de nuevo

n:    .data 4 ;número a calcular logaritmo
dos:  .data 2 ;base del logaritmo
uno:  .data 1 ;para ir aumentando el contador
resm: .data 2 ;aqui se guardara el resultado de la multiplicación
cont: .data 0 ;se guarda el resultado del logaritmo
``````

This code is tested in the simulator but it does nothing because it is not right.

To implement the | log2 x | I do not think it is difficult, the idea is to simply take the number x can be in `int` (32 bits) format, then extract from the most significant bit and check if it is `1` . If it is not, it continues to the next bit, and so on until it finds a one or ends with all the bits. The result of | log2 x | It will be the number of bits left from the first bit in one.

Some time ago I used assembler for micro-controllers, in those terms. I could say that it would be something like this in pseudo-code:

`````` 1. B = 32
2. Si B > 0 avanza al paso 3, en caso contrario sigue al paso 7
3. x << 1  ;corrimiento de bits a la izquierda
4. acarreo == 0 ? avanza al paso 5  en caso contrario avanza al paso 7
5. B-- ; resta uno a B
6. vuelve al paso 2
7. |Log2 x|= B
8. Fin
``````
Scroll to Top