HOW CHANGE PSEUDO CODE TO EMU8086 CODE?CAN HELP ME? - emu8086

Integer A,B
C=A2xB
IF(C=0)THEN C=C+1 ELSE C=AXORB

The answer to your question is simply to code.
pseudo code is made to be read by humans not computers, try think of it as instructions on what your program should do (or what you want it to do)
E.G
If 1 is equal to 2 then return 'three'
=>
if (1 == 2) {
return 'three'
}

Related

In swift, is there a way to only check part of an array in a for loop (with a set beginning and ending point)

So lets say we have an array a = [20,50,100,200,500,1000]
Generally speaking we could do for number in a { print(a) } if we wanted to check the entirety of a.
How can you limit what indexes are checked? As in have a set beginning and end index (b, and e respectively), and limit the values of number that are checked to between b and e?
For an example, in a, if b is set to 1, and e is set to 4, then only a1 through a[4] are checked.
I tried doing for number in a[b...e] { print(number) }, I also saw here someone do this,
for j in 0..<n { x[i] = x[j]}, which works if we want just a ending.
This makes me think I can do something like for number in b..<=e { print(a[number]) }
Is this correct?
I'm practicing data structures in Swift and this is one of the things I've been struggling with. Would really appreciate an explanation!
Using b..<=e is not the correct syntax. You need to use Closed Range Operator ... instead, i.e.
for number in b...e {
print(a[number])
}
And since you've already tried
for number in a[b...e] {
print(number)
}
There is nothing wrong with the above syntax as well. You can use it either way.
An array has a subscript that accepts a Range: array[range] and returns a sub-array.
A range of integers can be defined as either b...e or b..<e (There are other ways as well), but not b..<=e
A range itself is a sequence (something that supports a for-in loop)
So you can either do
for index in b...e {
print(a[index])
}
or
for number in a[b...e] {
print(number)
}
In both cases, it is on you to ensure that b...e are valid indices into the array.

How to activate an if statement with a range of numbers in swift

I am making a game where if a variable is equal to one number in a range of numbers an if statement will take effect. e.g. if var1 = 4, then the if will activate but it will also give an output if var1 was to equal 5, 6, 7 or 8 for example (the numbers in a set range).
I currently have no code for this part of the game so I won't be able to add it to this question.
Sorry if this question is badly explained or too vague to answer, I am new to stack overflow. Any help would be appreciated. Thank you
Probably the best way to do this is using Swift's powerful switch statements.
You can do this:
let x = 5
switch(x){
case 0..<4:
// Will match 0-3
print("one")
case 4..<10:
// will match 4-9
print("two")
case 10...:
// will match >= 10
print("three")
default:
print("other")
}
EDIT:
If that's too robust for your situation, you can also do this:
if (0..<4).contains(x){
print("yes")
}else {
print("no")
}
or even more simply:
if x >= 0 && x < 4{
print("yes")
}else {
print("no")
}

delete queue items inside foreach loop

When we need to delete some items inside a queue, we may easily write code like below:
foreach(queue[i]) begin
if(queue[i].value == 1)
queue.delete(i);
end
But there is bugs in above code when queue[0]==queue[1]==1. Because queue.delete(0) will change all indexes of items inside queue.
So currently I use code as below:
foreach(queue[i]) begin
if(queue[i].value == 1) begin
queue.delete(i);
i--;
end
end
It works, but it looks confusing at first glance.
So my question is:
Are there any better solution for this issue in system verilog?
I believe this should work (I'm unable to test it right now. Make sure order is persevered when you try it out)
queue = queue.find() with ( item.value != 1 );
Another approach would be to find all the indexes that meet your criteria, sort in depending order, then loop through the indexes
int qi[$] = queue.find_index() with ( item.value == 1 );
qi = qi.sort() with ( -item ); // sort highest to lowest
foreach(qi[idx]) queue.delete(qi[idx]);
Refer to IEEE1800-2012 ยง 7.12 Array manipulation methods for details

Binary Search without if-else statements

I want to write a binary search program in Scala without using any if-else statements at all. Basic strategy which i have devised uses the return value of comparison between array[mid] and search_key. In short :
1) Generate return value based on comparison between array[mid] and search_key
2) Create a unique string using that return value.
3) Call function using 'reflection' with help of that string
So my question is ...is there any computational logic which returns different values in this case ? How can i achieve this ? For example :
if array[mid] == search_key , return 0
if array[mid] > search_key , return 1
if array[mid] < search_key , return 2
If anyone have any better solution for this problem, please also suggest that.
The easy way that does something similar is
array(mid) compareTo search_key
This line is equivalent to
if (array(mid) == search_key) 0
else if (array(mid) < search_key) -1
else 1 // if (array(mid) > search_key)
As for the best way to do it, you could make a sequence of actions to take, and compute an index into that sequence. If you are interested you can see the full code in https://gist.github.com/kolmar/bcfc94ee4051ee7eb3a1

Finding the block using row and column indices - Sudoku Scala

I am new to Scala and am implementing a Sudoku solver. I have a method which returns the set of all possible values a particular element int the grid can take and it works. However, I think that there is a much better way to do this. The problem arises when I try to check the values of other elements in the same block. Is there any other way (than the one shown below) I can find a relationship between the row, column and block to result in cleaner code?
Note that r and c are the row and column indices, respectively, and are given as parameters to the function.
val i=
if(r==0|r==1|r==2){
if(c==0||c==1||c==2)
0
else if(c==3|c==4|c==5)
1
else
2
} else if (r==3|r==4|r==5){
if(c==0||c==1||c==2)
3
else if(c==3||c==4||c==5)
4
else
5
} else {
if(c==0||c==1||c==2)
6
else if(c==3||c==4||c==5)
7
else
8
}
def i(r:Int,c:Int) = r/3*3 + c/3
Although you'll probably want to find a better name/ add comments, it's not really the most intuitive function...