Representing and adding negative numbers in Easy68k Assembly - add

I'm trying to write a simple program in Easy68k that stores to negative values, adds them together, and then outputs them in the console. I am having trouble figuring out how to represent the negative numbers. We are asked that they be in hex format and output in decimal. Everything seems correct but the values themselves. I used 2s complement and then converted the two numbers to hex.
First decimal number = -102
Second decimal number = -87
Using 2s complement I converted the two numbers to hex (though I'm not sure if this is even correct):
-102 -> 1A
-87 -> 29
Here's my code so far:
addr EQU $7CE0
data1 EQU $1A
data2 EQU $29
ORG $1000
START: ; first instruction of program
* Put program code here
MOVE #data2,D1
MOVEA.W #addr,A0
ADD #data1,D1
MOVE D1,(A0)
MOVE.B #3,D0
TRAP #15
* Variables and Strings
* Put variables and constants here
END START ; last line of source
I even tried to just convert binary versions of the negative numbers straight to hex:
-102 -> 11100110 -> E6
-87 -> 11010111 -> D7
Which didn't work either. I also tried storing the binary version and adding them, but got the same result.
Here's the question:
Write a program in assembly to add the two numbers (-102 and -87). Inputs should be in hexadecimal format. Store the result in hexadecimal at an address $7CE0. Print out the result in decimal.(Hint: use the track function task #3). If an error happens, you should also print out the error message as well.
I know I am misrepresenting the two negative numbers, but I just can't figure out how to do it right. I've looked everywhere and found nothing on how to store/add/output negative numbers in 68k. Any help is appreciated, this is for an assignment so I'm not expecting direct answers. Thanks!

Related

The javascript thing is weird. I don't know if that happens

i found strange result of javascript
javascript
var a=123e65;
console.log(a);
javascript
result:1.23e+67 ;
why..this..?
I started studying JavaScript. During the study, I found strange results during various attempts. I can't figure out how to get that result...
Well, the result is correct. Its math: in your variable a, the value is 123e65 (that e before the 65 means "raised to the base 10". So the value 123e65 = 123x10^65).
In scientific notation, only numbers between 1 and 10 are valid. Ex: 1500 -> 1.5e3 or 1.5x10^3
in the value of your variable a, you are exponentiating a number greater than 10, (in this case, 123 exponentiated to 65). So javascript just adjusts that number, that is, it leaves 123 as a number between 1 and 10 (returning two places to the left 123 -> 1.23).
To compensate for this two-place return ( 123 -> 1.23) he adds two more numbers in exponentiation (65 -> 67).
So what used to be 123e65 or 123x10^65 became 1.23e67 or 1.23x10^67
This is because the value of variable is being converted to scientific format. Here, e represents '10 to the power'.
Hence, the output is coming as 1.23*10^67

How to convert a string to number in scheme without removing the leading zero?

I want to convert a string to a number in Scheme, but when I use the function string->number it removes the leading zero
For example
(string->number "01") gives me 1
Is there a way to convert the string so that it doesnt remove the leading zero and gives me 01 instead?
You can't: leading zeros are just part of the written representation of numbers, not part of the number itself. In particular 01, 1 and 00000001 are all the same number.
If you want to print numbers with leading zeros, for instance to line things up, then there are utilities which do that. For instance in Racket, while the format / printf family of procedures cannot do this, the procedures provided by racket/format can:
> (require racket/format)
> (~a 1 #:width 2 #:align 'right #:pad-string "0")
"01"
However you will still need to deal with negative numbers yourself, which is rather annoying.

Python digit formatting

I was taking some PCAP certification practice tests when I stumbled upon some sort of digit formatting, I don't really understand what it is and how it works.
Choose the correct statements from the following.
The output of print("-%07d"%555.55) is same as the output of print("%7d"%555.55)
The output of print("-%07d"%555.55) is same as the output of print("-07d"%555)
(Correct)
The output of print("-%02d"%555.55) is same as the output of print("-%2d"%555.55)
(Correct)
The output of print("-%02d"%555.55) is same as the output of print("-%02d"%555)
(Correct)
This is a simplified understanding of it.
The modulo or % character is the formatting character.
The d means integer only, so no floating point values will be used.
The - just makes it negative.
The integer before the d indicates the minimum number of characters desired in output.
If there is a 0 in front of the integer it means that it should be padded with leading zeros in order to reach the desired number of characters.
if the d was an f then it would allow floating point values and any zero padding would be done at the end

node: converting buffers to decimal values

I have a buffer that is filled with data and begins with < Buffer 52 49 ...>
Assuming this buffer is defined as buf, if I run buf.readInt16LE(0) the following is returned:
18770
Now, the binary representation of hex values 52 and 49 are:
01010010 01001001
If I were to convert the first 15 bits to decimal, omitting the 16th bit for two's complement I would get the following:
21065
Why didn't my results give me the value of 18770?
18770 is 01001001 01010010 which is your 2 bytes reversed, which is what the readInt*LE functions are going to do.
Use readInt16BE.
You could do this: parseInt("0x" + buf.toString("hex")). Probably a lot slower but would do in a pinch.

Conversion of numeric to string in MATLAB

Suppose I want to conver the number 0.011124325465476454 to string in MATLAB.
If I hit
mat2str(0.011124325465476454,100)
I get 0.011124325465476453 which differs in the last digit.
If I hit num2str(0.011124325465476454,'%5.25f')
I get 0.0111243254654764530000000
which is padded with undesirable zeros and differs in the last digit (3 should be 4).
I need a way to convert numerics with random number of decimals to their EXACT string matches (no zeros padded, no final digit modification).
Is there such as way?
EDIT: Since I din't have in mind the info about precision that Amro and nrz provided, I am adding some more additional info about the problem. The numbers I actually need to convert come from a C++ program that outputs them to a txt file and they are all of the C++ double type. [NOTE: The part that inputs the numbers from the txt file to MATLAB is not coded by me and I'm actually not allowed to modify it to keep the numbers as strings without converting them to numerics. I only have access to this code's "output" which is the numerics I'd like to convert]. So far I haven't gotten numbers with more than 17 decimals (NOTE: consequently the example provided above, with 18 decimals, is not very indicative).
Now, if the number has 15 digits eg 0.280783055069002
then num2str(0.280783055069002,'%5.17f') or mat2str(0.280783055069002,17) returns
0.28078305506900197
which is not the exact number (see last digits).
But if I hit mat2str(0.280783055069002,15) I get
0.280783055069002 which is correct!!!
Probably there a million ways to "code around" the problem (eg create a routine that does the conversion), but isn't there some way using the standard built-in MATLAB's to get desirable results when I input a number with random number of decimals (but no more than 17);
My HPF toolbox also allows you to work with an arbitrary precision of numbers in MATLAB.
In MATLAB, try this:
>> format long g
>> x = 0.280783054
x =
0.280783054
As you can see, MATLAB writes it out with the digits you have posed. But how does MATLAB really "feel" about that number? What does it store internally? See what sprintf says:
>> sprintf('%.60f',x)
ans =
0.280783053999999976380053112734458409249782562255859375000000
And this is what HPF sees, when it tries to extract that number from the double:
>> hpf(x,60)
ans =
0.280783053999999976380053112734458409249782562255859375000000
The fact is, almost all decimal numbers are NOT representable exactly in floating point arithmetic as a double. (0.5 or 0.375 are exceptions to that rule, for obvious reasons.)
However, when stored in a decimal form with 18 digits, we see that HPF did not need to store the number as a binary approximation to the decimal form.
x = hpf('0.280783054',[18 0])
x =
0.280783054
>> x.mantissa
ans =
2 8 0 7 8 3 0 5 4 0 0 0 0 0 0 0 0 0
What niels does not appreciate is that decimal numbers are not stored in decimal form as a double. For example what does 0.1 look like internally?
>> sprintf('%.60f',0.1)
ans =
0.100000000000000005551115123125782702118158340454101562500000
As you see, matlab does not store it as 0.1. In fact, matlab stores 0.1 as a binary number, here in effect...
1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 1/65536 + ...
or if you prefer
2^-4 + 2^-5 + 2^-8 + 2^-9 + 2^-12 + 2^13 + 2^-16 + ...
To represent 0.1 exactly, this would take infinitely many such terms since 0.1 is a repeating number in binary. MATLAB stops at 52 bits. Just like 2/3 = 0.6666666666... as a decimal, 0.1 is stored only as an approximation as a double.
This is why your problem really is completely about precision and the binary form that a double comprises.
As a final edit after chat...
The point is that MATLAB uses a double to represent a number. So it will take in a number with up to 15 decimal digits and be able to spew them out with the proper format setting.
>> format long g
>> eps
ans =
2.22044604925031e-16
So for example...
>> x = 1.23456789012345
x =
1.23456789012345
And we see that MATLAB has gotten it right. But now add one more digit to the end.
>> x = 1.234567890123456
x =
1.23456789012346
In its full glory, look at x, as MATLAB sees it:
>> sprintf('%.60f',x)
ans =
1.234567890123456024298320699017494916915893554687500000000000
So always beware the last digit of any floating point number. MATLAB will try to round things intelligently, but 15 digits is just on the edge of where you are safe.
Is it necessary to use a tool like HPF or MP to solve such a problem? No, as long as you recognize the limitations of a double. However tools that offer arbitrary precision give you the ability to be more flexible when you need it. For example, HPF offers the use and control of guard digits down in that basement area. If you need them, they are there to save the digits you need from corruption.
You can use Multiple Precision Toolkit from MATLAB File Exchange for arbitrary precision numbers. Floating point numbers do not usually have a precise base-10 presentation.
That's because your number is beyond the precision of the double numeric type (it gives you between 15 to 17 significant decimal digits). In your case, it is rounded to the nearest representable number as soon as the literal is evaluated.
If you need more precision than what the double-precision floating-points provides, store the numbers in strings, or use arbitrary-precision libraries. For example use the Symbolic Toolbox:
sym('0.0111243254654764549999999')
You cannot get EXACT string since the number is stored in double type, or even long double type.
The number stored will be a subtle more or less than the number you gives.
computer only knows binary number 0 & 1. You must know that numbers in one radix may not expressed the same in other radix. For example, number 1/3, radix 10 yields 0.33333333...(The ellipsis (three dots) indicate that there would still be more digits to come, here is digit 3), and it will be truncated to 0.333333; radix 3 yields 0.10000000, see, no more or less, exactly the amount; radix 2 yields 0.01010101... , so it will likely truncated to 0.01010101 in computer,that's 85/256, less than 1/3 by rounding, and next time you fetch the number, it won't be the same you want.
So from the beginning, you should store the number in string instead of float type, otherwise it will lose precision.
Considering the precision problem, MATLAB provides symbolic computation to arbitrary precision.

Resources