Create a loop that "displays" the bits in ax. The word "displays" means to put the bits into another register. Values in the other register is 0 or 1

user16567153

Assembly language programming

Store a value in ax. Create a loop that "displays" the bits in ax. The word "displays" means to put the bits into another register. Values in the other register must be only 0 or 1.

I am doing it like this but I don't know I am doing it correctly or not. Please help.

.data 

x:
     .word 0b00101

.text                       

.globl _start                
                            

_start:  

     movw x, %ax
loop:
     
     cmp %ax, %bx
     
     je done
     
     andw 0b1000000000000000, %ax
     
     shl $1, %ax
     
     jmp loop
     
done:
     nop
Sep Roland

Please use SHL, and AND instruction only

The trouble with this kind of condition is that we can't know what other instructions you are allowed to use. Your own example already uses MOV, CMP, JE, JMP, and even NOP.
Because the task asks for a loop with precisely 16 iterations, it's clear that it's just impossible to only use SHL and AND. We need at the very least a conditional jump and use an independent iteration counter.

The basic idea is always that SHL puts the bit that it shifts out, in the carry flag. From there, an instruction like ADC can pick it up and produce the result BX=1.

; IN (ax)
  mov  cx, 16
again:
  xor  bx, bx   ; BX=0
  shl  ax, 1    ; -> CF
  adc  bx, bx   ; BX=[0,1]
  loop again

Now I'll remove those XOR, ADC, and LOOP instructions that you yourself didn't use. This example only uses MOV, SHL, and JNC:

; IN (ax)
  mov  cx, 1
again:
  mov  bx, 0
  shl  ax, 1
  jnc  cont
  mov  bx, 1
cont:
  shl  cx, 1    ; Produces CF=1 after 16 iterations
  jnc  again

Provided that the value in AX has its lowest bit set (like in your example 0b00101), we can do without the independent iteration counter.

; IN (ax)
again:
  mov  bx, 0
  shl  ax, 1
  jnc  cont     \
  mov  bx, 1    | These don't change flags
cont:           /
  jnz  again    ; Still based on the flags from `shl ax, 1`

What will happen if you use this snippet on an AX value that does not have its lowest bit set, is that the loop will have less than 16 iterations!


And if you feel the urge to use the AND instruction at least once, you can use it to clear the BX register:

; IN (ax)
  mov  cx, 1
again:
  and  bx, 0    ; ANDing BX with zero produces BX=0
  shl  ax, 1    ; -> CF now has the bit that was shifted out at the high end of AX
  jnc  cont     ; That bit was 0, so BX=0 is fine
  mov  bx, 1    ; Else make BX=1
cont:
  shl  cx, 1    ; Produces carry after 16 iterations
  jnc  again

On successive iterations CX will hold 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 0
When the value finally becomes 0, the CF will get set because the true result of doubling the value (65536) no longer fits the 16-bit register.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set all bits in CPU register to 1 efficiently

How to move bits of a number from register to eight other registers

How to exchange certain bits of the register

How to display decimal equivalent (0-63) on two 7-segment displays using 6 switches as bits?

PIC programming which bits are which in TRIS register

How bits of this register is set in u-boot

Where are displays other than ":0"?

how to set control register 0 (cr0) bits in x86-64 using gcc assembly on linux

_BitScanReverse returns 0 when index is 1 which means according to MS "no set bits were found"

How to MOVe 3 bytes (24bits) from memory to a register?

How can I toggle certain bits in an 8bit register?

Smali:All register args must fit in 4 bits error

Fastest way to move higher or lower 64 bits in integer SSE register

Extract bits of a register starting with the most significant bit, or a high bit

MOV to register from memory not working with BITS 32 in nasm

Select one pin from 16 bits wide register, python

Verilog - Read bits of register dynamically or using some variable

Write a specific bit in a 16bits register with modbus in python

AVX512 exchange low 256 bits and high 256 bits in zmm register

mips extract certain bits from register and set them to a different register in the same place without changing the rest

How to create a histogram that displays counts of binomial values with respect to a specified range of another variable

trying to loop through an array of images but code only displays the number 1 (not even number 0)

Create new column by sampling bits of other columns

I am trying to use a for loop in javascript to create a a table that displays * and for each row the columns increase by 1 *

What Does Shifting Variables OR REGISTERS means ? 1<<REGISTER_ADDRESS

Sum a byte array to ax register

Set all bits in unsigned to 1 if equal, 0 if not

Sequence of 1's and 0's python bits

String of 0's and 1's to File as bits