How do you clear a specific bit in a register without changing the other bits in ONE instruction? - hex

For example, let's say register 4 (R4) has a value 0001110010101111. How could you change bit 5 (0001110010 >1< 01111) to 0 (even if it was already 0) without moving or changing the other bits in a single hex instruction?
So 0001110010101111 -> 0001110010001111

You'll want to AND it. Since the immediate value for AND is 5 bits and it uses sign extension, you can only clear a bit if it's one of the four least significant bits. Otherwise, you will need to perform another instruction to load the mask into a register. I'll do an example of both.
In the case of the 5th bit, the number that will mask the bit is 0b1111111111011111. In decimal, this is #65503 or #-33. Since this is too big to fit in an immediate instruction, you won't be able to do it in a single instruction. You will need to declare it in the data segment of your program and load the mask into a register. Then, you can AND it with R4.
; assuming R4 = 0001110010101111
LD R5, MASK_5 ; load the mask into R5
AND R4, R4, R5 ; set R4 = R4 AND R5
; R4 will now have the bit cleared
; data segment
MASK_5 .FILL #65503
In the case of the 3rd bit, the number that will mask the bit is 0b1111111111110111. In decimal, this is #65527 or #-9. This will fit in the immediate value of AND, so you can perform it in a single instruction:
; assuming R4 = 0001110010101111
AND R4, R4, #-9 ; set R4 = R4 AND #-9
; R4 will now have the bit cleared

Related

neo4j: extraction of random graphs

We have a big graph database made with Neo4j which has two type of relationships "E" and "I".
We would like to extract two graphs from it with a starting node called n0.
The first graph Gxi, based on the "I" relationship, must be obtained randomly.
The following request is wrong but this is the idea we want to implement. Here 10 neighbors are randomly chosen for each node of the last step
MATCH r1:(n0)-[:I]-(n1)
WITH random(n1) LIMIT 10
MATCH r2:(n1)-[:I]-(n2)
WITH random(n2) LIMIT 10*10
MATCH r3:(n2)-[:I]-(n3)
WITH random(n3) LIMIT 10*10*10
MATCH r4:(n4)-[:I]-(n4)
WITH random(n4) LIMIT 10*10*10*10
RETURN r1+r2+r3+r4
Then we would like to create the second graph Gxe based on the relationships "E" and the nodes of Gxi.
Thank you for your help.
APOC Procedures may be able to help here. There are collection functions that can be used to choose random items from a collection, and you can get slices of the collection rather than having to use LIMIT.
The trickier part will actually be collecting the subpaths along the way.
// assume already matched to start node n
MATCH r = (n)-[:I]-()
WITH apoc.coll.randomItems(collect(r), 10) as r1
UNWIND r1 as r
WITH r1, last(nodes(r)) as n
MATCH r = (n)-[:I]-()
WITH r1, apoc.coll.randomItems(collect(r), 10) as r2
UNWIND r2 as r
WITH r1, r2, last(nodes(r)) as n
MATCH r = (n)-[:I]-()
WITH r1, r2, apoc.coll.randomItems(collect(r), 10) as r3
UNWIND r3 as r
WITH r1, r2, r3, last(nodes(r)) as n
MATCH r = (n)-[:I]-()
WITH r1, r2, r3, apoc.coll.randomItems(collect(r), 10) as r4
RETURN r1 + r2 + r3 + r4

Intel 8086 Division. Need AX to be denominator not numerator

Need to divide CL by 10. I understand that in division AX will always be the numerator, but I need it to be the denominator.
In the code below I attempt to move the value I need as the numerator into AL and then put 10 into CL and divide.
Any help would be appreciated.
Mov Al, Cl
Mov Cl, 10
Div Cl
Need to divide Cl by 10
So you'll want the quotient back in the CL register then.
Your corrected solution (10 bytes):
mov al, cl
xor ah, ah ;Clear high byte because DIV will use the entire AX
mov cl, 10
div cl ;Divides AX by 10, leaves quotient in AL
mov cl,al
Shorter solution (6 bytes):
mov al, cl
aam ;Divides AL by 10, leaves quotient in AH
mov cl, ah
On 8086 the AAM instruction exclusively divides by 10.
On x86 the AAM instruction can divide by any byte-sized number.
So this AAM instruction is in effect an 8 bit by 8 bit division!
Is there a way then to divide a register by 10?
All divisions implicitly use the accumulator (AL, AX, DX:AX). Just copy your register to the accumulator first.

How can I check if a register is evenly divisible by 7?

I want to check if the value of the register ax is divisible by 7, with remainder = 0. How can I do it?
You can use the same way as shown in this answer, with different constants of course.
The modular multiplicative inverse of 7 mod 216 is 0x6db7, which (by definition) means that numbers of the form x = n * 7 will obey x * 0x6db7 = n, where n will be less than or equal to 0xffff / 7. So if you get something bigger out of x * 0x6db7, you know it's not a multiple of 7. Also, non-multiples of 7 cannot also map to low results, because multiplication by an odd number modulo a power of two is bijective.
So you can use (not tested)
imul ax, ax, 0x6db7
cmp ax, 0x2492
ja not_multiple_of_7
This is for unsigned numbers of course.
As a bonus, ax will be the original value divided by 7 iff it was a multiple of 7.
org 100h
mov ax,14
mov cl,7
div cl
cmp ah,0
je positive
PRINTN "The number has a remainder"
jmp finish
positive:
PRINTN "The number has no remainder"
finish:
PRINTN "After comparison"
mov ah, 0
int 16h
ret

Read After Write(RAW) HAZARD

I am confused in finding RAW dependencies whether we have to find only in adjacent instructions or non-adjacent also.
consider the following assembly code
I1: ADD R1 , R2, R2;
I2: ADD R3, R2, R1;
I3: SUB R4, R1 , R5;
I4: ADD R3, R3, R4;
FIND THE NUMBER OF READ AFTER WRITE(RAW) DEPENDENCIES IN THE Above Code.
assume ADD x,y,z = x <- y + z
I am getting 2 dependency I2-I1 and I4-I3.
Let us say that after an instruction enters the pipeline, it will take it x stages after which any register write by that instruction will be visible to any following instruction.
Then you have to take care of the RAW dependencies among every set of x consecutive instructions. In the worst case you can take x to be the max no. of stages in the pipeline.
Now, the case in the question looks like a HW problem and since the pipeline structure is not defined so you will have to look at the RAW dependencies over all the instructions, which in this case are:
I2 and I1 over R1
I3 and I1 over R1
I4 and I2 over R3
I4 and I3 over R4

8086 assembly language unsigned interpretation

I have to write a program in 8086 assembly that calculates this:
(a+b*c+2/c)/(2+a)+e
where
a,b - byte
c - word
e - doubleword,
in unsigned interpretation.
So far I have this:
assume cs:code,ds:data
data segment
a db 4
b db 2
c dw 16
e dd 126
data ends
code segment
start:
mov ax,data
mov ds,ax
and here is my program
mov al,b ; al=b
mov ah,0 ; ax=b;
mul c; dx:ax=b*c; b*c=doubleword
mov bx,ax; we save b*c in bx to use ax in the division 2/c
mov al,2; al=2
mov ah,0; al=ax=0
div c; ax=dx:ax/c ; dx=dx:ax%c;
I don't know how to continue.
When you write
mov bx,ax; we save b*c in bx to use ax in the division 2/c
you're actually only saving the low word of the product between B and C. It works with the numbers provided but it's not a general solution.
In stead of writing
mov al,2; al=2
mov ah,0; al=ax=0
use mov ax,2 and remember to explicitely clear the DX register because the division needs it.
To continue I would suggest you create a result variable of doubleword size because in the end the size of your largest expression participant defines the size of the result. Then on the way move or add your partial results to this variable.
If you are allowed to use 32 bit registers a suitable approach might be to promote all values to 32 bit and continue from there.

Resources