NASM using expressions - nasm

In chapter 3.5 of the NASM manual, there are definitions for a bunch of expressions: https://www.nasm.us/doc/nasmdoc3.html
I can't seem to use those though. Arithmetic works;
db (10 / 5)
outputs a byte with value 2, as expected. But I cannot figure out how to use the other operators, like the > and ?:
db (10 % 4) > 0
should create a byte with value 1, but instead when I try to compile it, it gives me error: comma expected after operand 1. Same if I try to use the ?:
db (10 % 4) > 0 ? 10 : 20
same error.
How do I use these operators?

Related

Need explanation to translate

I am currently learning python, but also other programming languages. Now I've run into a dilemma. I have the following code that i need to translate to three different programming languages, but before I do that I want to know in detail how the code works. The code (in python) is :
my_secret_key = 5
def decrypt(secret_string, secret_number):
result = ""
for x in secret_string:
result = result + chr(ord(x) ^ int(secret_number))
return result
print(decrypt("alq%lv%``k%b`m`lh", my_secret_key))
Now I have basic knowledge of Python and understand parts of this code, but the calculation it is making is difficult for me to understand.
Are there people willing to help me translate this code to human language so I can then find my way to translate this into other programming languages.
Thanks!
I have tried to look up the functions of chr and ord, and combined this with the x ^ int(secret_number). I tried to devide the code into blocks which helped me understand parts of it.
I tried just applying the calculation blocks but that doesnt work, it needs the full code to work
Explained with comments above each line:
def decrypt(secret_string, secret_number):
result = ""
# for each character in the encrypted string:
for x in secret_string:
# 1. convert the character to `int` using the `ord` function
# 2. convert the secret_number to `int` using the `int` function (this
# is redundant is this example, as `my_secret_key` is already `int`)
# 3. XOR the two integers with each other
# 4. convert the result from `int` back to `char` (actually 1-element
# string) using `chr`, and append it to the result string
result = result + chr(ord(x) ^ int(secret_number))
# return the decrypted string
return result
Regarding the ^ operator, from Python docs:
The ^ operator yields the bitwise XOR (exclusive OR) of its arguments [...]
This means that the result is an integer, whose binary representation will have:
zeros on bit positions where both arguments had 0, or both arguments had 1
ones on bit positions where one of the arguments had 0, and the other had 1
Example:
argument 1: 001 (binary), 1 (decimal)
argument 2: 011 (binary), 3 (decimal)
---
XOR result: 010 (binary), 2 (decimal)
^
bits in argument 1 and 2 on this position have different values,
so the result is 1
You can find more info on the XOR operation e.g. on Wikipedia: Exclusive or, XOR Cipher

Why does 'is' operator show different result in Python - 3.6.8?

Referred following question -
1) different run results between python3 and python2 for the same code
2) “is” operator behaves unexpectedly with integers
3) Consecutive 'is' operator in Python [duplicate]
4) Chaining “is” operators
5) What does 'is' operator do in Python?
6) Strange python 'is' operator in arithmetic [duplicate]
7) Why does the “is” keyword have a different behavior when there is a dot in the string?
Explanation for not duplicate question - In my question, I have not included any symbol, numeric values. I am including simple string. In above 7th referred question, I already know the reason behind giving different output and I have already read that.
Here, my question is different.
My Question Description -
I am practicing following Python code in 3.6.8 version and using PyCharm editor.
print('a' * 18 is 'aaaaaaaaaaaaaaaaaa')
print('a' * 19 is 'aaaaaaaaaaaaaaaaaaa')
print('a' * 20 is 'aaaaaaaaaaaaaaaaaaaa')
print('a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa')
print('a' * 22 is 'aaaaaaaaaaaaaaaaaaaaaa')
Output =
True
True
True
False
False
Why does 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa' and onwards strings not evaluate to True?
Any help would be appreciated
Why does 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa' and onwards strings not evaluate to True?
From following diagram, The penultimate source code compilation step produces a first version of bytecode. This “raw” bytecode finally goes into a last compilation step called “peephole optimization”. The goal of this step is to produce more efficient bytecode by replacing some instructions by faster ones.
Reference Image
The .pyc files encounter in all Python packages. Python bytecode is stored in these files. What would happen if someone wrote something like this ['foo!'] * 10**9? The resulting .pyc file would be huge! In order to avoid this phenomena, sequences generated through peephole optimization are discarded if their length is superior to 20.
So, in above question, following given code produces False result for the value of length superior to 20 and not produces False for value 20 or less than 20 for string.
print('a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa')
print('a' * 22 is 'aaaaaaaaaaaaaaaaaaaaaa')
output =
False
False

Comparison between strings and integers in matlab

I am doing some classification and needed to convert an integer code to strings for that reason. I wrote something like this:
s(1).class = 1;
s(2).class = 7;
s(3).class = 9;
[s([find([s.class] == 1)]).class] = deal('c1'); %first conversion
[s([find([s.class] > 1)]).class] = deal('c2'); %second conversion
and was surprised to find s being a 1x4 struct array after the second conversion instead of the expected 1x3 struct array with the values.
Now, after some research, I understand that after the first conversion the value of s(1).class is 'c1' and the argument to find in the second conversion is not what I assumed it would be. The [s.class] statement actually returns something like the string 'c1\a\t' with ASCII escape sequences for bell and horizontal tab.
As the comparison does work (returning the matrix [1 1 1 1] and thus expanding my structure) I assume that matlab converts either the operand [s.class] or the operand 1.
Which is it? What actually is compared here numbers or characters?
And on the other hand is there a built in way to make > more restrictive, i. e. to require the operands to be of the same type and if not to throw an error?
When you do the comparison 'ab' > 1, the char array 'ab' gets converted to a double array, namely the ASCII codes of the characters. So 'ab' > 1 is equivalent to double('ab') > 1, which gives [1 1].
To get the behaviour you want (issue an error if one of the arguments is char) you could define a function:
function z = greaterthan(x,y)
if ischar(x) || ischar(y)
error('Invalid comparison: one of the input arguments is of type char')
else
z = x>y;
end
so that
>> greaterthan([0 1 2], 1)
ans =
0 0 1
>> greaterthan('ab', 1)
??? Error using ==> greaterthan at 3
Invalid comparison between char and int
Because you have not provided any expected output yet, I am going with the observations.
You are using a comprehension method (by invoking find) to determine which locations you will be populating for struct s with the results from your method deal (takes the argument c1 and c2). You have already set your type for s{whatever).class in the first snippet you provided. Which means it is number you are comparing, not character.
There is this isa function to see which class your variable belongs to. Use that to see what it is you are actually putting in (should say int32 for your case).

Error with floating point / scientific notation values in bash

I have a problem with the following piece of code
THRESH_SERIE=("1" "5E-1" "1E-1" "5E-2" "1E-2" "5E-3" "1E-3" "5E-4" "1E-4")
for ((i=0;i<${#THRESH_SERIE[#]};i++))
do
let thresh=$(echo ${THRESH_SERIE[$i]})
$EXEC 1 $N ${thresh} 0 0 >> $OUTPUT
done
If I try to run the script it will return an error like the following for each value in the array:
/bench_new.sh: line 40: let: thresh=5E: value too great for base (error token is "5E")
I've tried also to use floating point numbers (like "0.1"), but it gives a different error. How can I solve this?
No particular reason for the array here (other than they're "fun" to play with):
THRESH_SERIE="1 5E-1 1E-1 5E-2 1E-2 5E-3 1E-3 5E-4 1E-4"
for thresh in ${THRESH_SERIE}
do
$EXEC 1 $N ${thresh} 0 0 >> $OUTPUT
done
The reason this is borking is that let treats all it's arguments as arithmetic values.
bash Arithmetic expression are defined by a set of rules which default to decimal. the E is not a decimal number and hence the error.
as #DiegoBasch suggests try dropping the let so it's not treated as an arithmetic expression.

Haskell: lexical error in string/character literal at character 'i'

I'm fairly new to Haskell programming and I'm having trouble understanding why I'm receiving this error in my code.
My problem is as follows: Any positive integer i can be expressed as i = 2^n*k, where k is odd, that is, as a power of 2 times an odd number. We call n the exponent of 2 in i. For example, the exponent of 2 in 40 is 3 (because 40 = 2^3*5) whereas the exponent of 2 in 42 is 1. If i itself is odd, then n is zero. If, on the other hand, i is even, that means it can be divided by 2. Write a function exponentOfTwo for finding the exponent of 2 in its argument.
I understand the psuedocode and it seems fairly simple: recursively divide i by 2 until result is odd, the number of times the division happens is n
here is my code (line 31-32):
exponentOfTwo :: Int -> Int
exponentOfTwo i = if odd i then 0 else 1 + exponentOfTwo (i 'div' 2)
I'm receiving the error "lexical error in string/character literal at character 'i'" on line 32 column 62.
I've tried searching for a solution to this error everywhere and so far I've had no luck.
To use a function in infix for, surround it with backticks (`), not with single quotes ('). The latter are for character literals, which, well are only one character long.
Are the characters around div backquotes rather than normal quotes? They need to be to allow a function name to be used as an infix operator. I changed that in your definition and the code worked for me.

Resources