0

I'm implementing a code in IAS assembly that solve this problem:

I have an array of integers A=[a1,a2,...,an] and I have to calculate B=[|log2 a1|,|log2 a2|,...,|log2 an|], where || is the floor function that rounds down to the nearest integer.

I'm trying to implement the following steps:

  1. create first the |log2 x| and verify that it works for a positive integer
  2. run 1) in each number of the array A to calculate the array B

I wrote this, but it doesn't work:

loop:   S(x)->Ac+ n ;load n in AC
        Cc->S(x) log ;if AC >= 0 jump to log
        halt            ; else end the program
        .empty

log:   S(x)->R resm  ;copy number 2 to AR
       S(x)*R->A two ;multiply 2*2 
       At->S(x) resm ;save in resm
       S(x)->Ah+ one ;+1 counter
       At->S(x) cont ;save the counter
       S(x)->Ac+ n ;load n in AC
       S(x)->Ah-  one;decrease n in 1
       At->S(x)   n ;save n 
       Cu->S(x)   loop; jump to beggining to make all again

  n:    .data 4 ;number to calculate log
  two:  .data 2 ;base of the logarithm
  one:  .data 1 ;for increase the counter
  resm: .data 2 ;for save the result of the multiplication
  cont: .data 0 ;save the result of the logarithm

IAS is a teaching teaching language, implemented in a simulator. That page also documents the instruction set.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Marco Araya
  • 23
  • 1
  • 5
  • You need to describe *how* it doesn't work, i.e. what happens when you run it, and what you found out with a debugger. The question must be answerable without having to run your code; see this explanation of creating a [mcve] – Peter Cordes Jun 24 '16 at 05:20
  • oh , well ,i dont know how to solve my problem because dont understand very well how to program in assembler even in this simulator, when i assemble my code the simulator show an error ," ERROR :Labels can only be used with instructions in the left half of a word at line: 20 and column: 0" – Marco Araya Jun 24 '16 at 05:53
  • And line 20 is the `cont: .data 0` line? You should make an edit to mark it in your question. I had to paste your code block into `cat -n` to get line numbers. – Peter Cordes Jun 24 '16 at 05:59

1 Answers1

0

Since the instruction set doesn't have a lzcnt / bsr type of instruction to do this without looping, the normal way would be to count how many right-shifts it takes to shift out all the bits.

floor(log2(x)) is the same thing as finding the position of the most-significant bit:

e.g. floor(log2(17)) = floor(log2(16)) = 4, and floor(log2(15)) = 3.
17 and 16 both have 5 binary digits, but 15 is only 0b1111 (4 binary digits).

Since log2(0) is -Infinity, the function is not well defined if the input is zero. Even x86's bsr instruction leaves the result undefined in that case, unfortunately.

I think you're trying to loop by shifting a 1 left until it's greater than or equal to x, based on the multiply by two. That will work. (a left shift is a multiply by 2).

Your comment says something about 2 * 2, which is weird.

I don't know IAS, and you didn't say anything about HOW your code doesn't work. So that part of the question is still up to you until you update the question to ask anything specific about one instruction not doing what you expect, or something.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847