Question:
Assembly Programming Question?
- DP -
2010-09-12 18:48:13 UTC
I'm working on a project dealing with the Assembly language. I'm new to it so I don't get the syntax very much (I've programmed before in C, C++, and Java).

The assignment is to create a program that takes in two strings and finds the Hamming distance (the number of bit positions where the two strings differ). For example, if I were to compare the strings:
"f" = 0110 0110
and
"a" = 0110 1111

the hamming distance would be 2.

The part I've accomplished so far is storing the strings into variables. However, I'm confused as to how I will convert the characters into ASCII bits for comparison. I also don't know how I'm supposed to go about storing the counter for the comparisons (I know there will be a loop used).

A few specifications for the program include:
if the user uses two strings with different lengths, the distance up to the length of the shorter string should be returned.
The maximum length of a string can be 255 characters.


Please help!

Thank you!
Three answers:
mdigitale
2010-09-12 19:15:04 UTC
Alright, so you have two strings stored in variables and you want to find their hamming distance.



This is the approach I would take:



You know that the maximum string length is 255, but you only need to worry about the length of the shortest string. So I would find the length of the two strings and whichever was shorter, use that as the sentinel for my loop.



Since you mentioned ASCII, I will assume that your string is comprised of bytes -- 8 bit quantities. Therefore, inside the loop you will want to work with the data on a byte level (e.g. al instead of eax, ax, etc.)



Take the byte in string A and XOR it with the corresponding byte in string B. The result will have 1 set in the bit positions where the bytes differ. Now you need to figure out how many 1's there are in the result -- there are many ways to do this. One of the easiest is to use a progressive bit-mask, testing for one in each position, keeping a count. In other words, you will need another sub-loop that iterates through the 8 bits for each byte.



Keep the totals as you march through the string. When the outer loop is exhausted you are done.



Good luck.
2010-09-12 19:14:15 UTC
1) "ASCII bits" is two words that belong together like ham and hamming - they don't. The bits are what they are. (IOW, if you put an "A" into a variable, you have 01000001 in the variable. Those are the bits. That's ASCII for "A". ["a" is 0110 0001])



2) Use a register for the counter.



3) FYI, "a" is a character, not a string. There's a difference, usually dictated by a high level language's definition of "string".
dickensheets
2016-11-16 15:56:44 UTC
Why no longer attempt yet another language first, like C? assembly is problematic. that's a low point language, meaning dissimilar artwork for little or no result. And coming up an working equipment is rather problematic, you will possibly desire to be an experienced programmer earlier commencing with something like that. Given the actual fact you don't be attentive to hexadecimal, that's trivial understanding, you don't be attentive to something approximately programming in any respect i think. i could recommend you first study to application. C is an fairly solid language, and you will choose it to your working equipment later on besides, so that's no longer time wasted.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...