• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help Me Assembly Coding Gods!

Dragnov

Diamond Member
This is my first time ever hearing or even (trying to) code Assembly so all help would be appreciated...

I'm supposed to convert a string of numbers into a decimal value... So I'm assuming that I should convert the hexidecimal values into an integer string and then after that, the integer string to a decimal string.

How am I supposed to get the first hexidecimal value ONLY and then how would I make that into an Integer? For example, "123" will be [31h][32h][33h][0h] in memory. So how would I just get the [31h] and what would I need to do to make that into an integer?

I'm assuming I could just loop it to convert the next hexidecimal value after and then store the integer value after.

This is what I think/tried so far...

Can I just move the String into EAX? And then use the edp to pick the memory addresses? For example, to get to [31h]... edp + 8, and then [42h]... edp + 16, and so on? And then even after getting to the hexidecimal values, how the hell would I convert it to an integer? sub? add? mul? div? shl? I'm clueless in this category.

All I know for sure is that I have to move the string into some location memory.. be it eax, ax, bx, etc. Where I have no clue, how to access it once in there I have no clue again, and to make the conversion I have no clue also. :disgust:😱

Pleae don't write out the code for me. 😛 Don't want to get kicked out of school and all for plagarism, nor will I learn anything. Few snippets will be okay if it'll explain.

Once again, I'm brand new to this. Thanks in advance.😎

<~~~ Hopeless. Furstrated. Tired. Has until Tuesday till 5 to finish this.
 
arrr! assembly language! You might be able to find a better answer in Software and Programming or maybe even Highly Technical seeing as this is assembly language. If anyone knows asm, then you've got better things to do than surf atot. which is why i think you wont be able to find your answer here.
 
All processors have different instruction sets. It matters a great deal which chip you're programming for. There's a reason that you can run programs for a sparc station on a macintosh 🙂

Anyway, I've only done MIPS assembly, never x86 (and I'm not exceptional at MIPS, either)
 
16 bit should do I guess. If not I could at least learn from it.

OS/Tools? Using Microsoft Visual C++ to compile the code....
 
OK, do you know how to code the program using C/C++? The first step that I used to do when I did assembly programs was to code the program using the _simplest_ C++ code possible.
 
first step is breaking off one of the hex values by anding w/ a mask
123 = 313233
mask = FF0000
and value = 310000

shift right so it's just 31 and subtract 30 to each hex result to get the integer equiv


sorry if this is totally wrong, i'm really tired and havn't touched assembly code for several years
 
Operating on the assumption that you have the string already in memory, Yoiu need to load a register with the address of the string. Lets call this register R-A
Initialize an output register to zero. Lets call this register R-O

Then looping for the length of the string
Load an additional register with the data pointed to by R-A. Lets call this register R-D
subtract the hex value of ascii zero from R-D
multiply R-O by 10
add R-D to R-O
increment R-A to point to the next character in the string.
go to the top of the loop
 


<< Operating on the assumption that you have the string already in memory, Yoiu need to load a register with the address of the string. Lets call this register R-A
Initialize an output register to zero. Lets call this register R-O

Then looping for the length of the string
Load an additional register with the data pointed to by R-A. Lets call this register R-D
subtract the hex value of ascii zero from R-D
multiply R-O by 10
add R-D to R-O
increment R-A to point to the next character in the string.
go to the top of the loop
>>



Thanks, that helps a lot. Now I have to convert the string of base 10 into a string of Base X. They told me I need to use a stack and push and pop and stuff. But thats too broad for my knowledge. Anymore help I can get for this one guys? Thanks.
 


<< Sorry. 80x86 processors. 32 bit. 😱 Didn't know it mattered that much... told you I'm clueless. 😛 >>



clueless is an understatement, good luck 😀
 


<< Thanks, that helps a lot. Now I have to convert the string of base 10 into a string of Base X. They told me I need to use a stack and push and pop and stuff. But thats too broad for my knowledge. Anymore help I can get for this one guys? Thanks. >>



Have you coded the first one? Is it working ok? Because you need that procedure for this next part. For converting into base X, you first need to convert the string to a number. Then you need a loop to process the number using the base. Here's an example:

We will use division and modulus (% operator) to convert the number to a base X string. Suppose the base is 16 => 0x10; the number to convert is 267. In each step, we calculate a "new number" which becomes the number that we're dealing with. We would go through the following steps:

292 % 16 = 4 => 4 in ASCII -> push on stack
292 / 16 = 18 => new number

18 % 16 = 2 => 2 in ASCII -> push on stack
18 / 16 = 1 => new number

1 % 16 = 1 => 1 in ASCII -> push on stack
1 / 16 = 0 => new number

We stop when the "new number" hits 0. The result is on the stack, which you need to pop to get the correct answer. So if you popped the result, your answer would be "124" in hex - which is the right answer. You need the stack because we calculate the string in the opposite order, so if you use the stack, you get the result back correctly.

This doesn't support negative numbers, so if you want to deal with negative numbers, simply print a negative sign, and make the number positive for the calculations.
 
Hey thanks guys. I actually got the first part working.. at least I believe so. (Only took me 3 days/nights!)😀 Now I'm working on the second one still... I'll give it a go with singh 's advice. Thanks again.
 
Your stringToDecimal needs exactly 1 input and 1 output. You PUSH the input string (address) on the stack and then call stringToDecimal because the stringToDecimal function will use it as as an input. Once you're done, you need to return the output - the decimal number.

For example, suppose your stringToDecimal function is like this:

int stringToDecimal(char *string)
{
int number;
// .. some code

return number;
}

When the function returns, the return value is passed in register EAX.

Suppose you call the function from assembly:

mov eax, offset in_str
push eax /* Push the in_str's address onto the stack */
call stringToDecimal
pop ebx /* Restore stack */

/* Return value from stringToDecimal is in EAX
* Use it as you need
* ex)
*/
mov numInBase10, eax

/* Now convert to basex, using out_str to store the result */




 
Eh? Thanks for your help, but I'm so confused. 😕 Sorry. Here's my entire block of code... it works for jsut conversions from string to decimal but not when I want to do base X.

int stringToDecimal( char* in_str )
{
int return_val;
short ten = 10; //use this to multiple by. Note that it's 2 bytes long

__asm
{

mov eax, 0 //clears out eax, abx, and ecx
mov ebx, 0
mov ecx, 0

mov ebx, in_str //puts in the character string into ebx

conversionLoop: //function to do the conversions

mov ecx, 0
mov cl, [ebx] //gets a byte from the string
cmp cl, 0 //checks to see if the value is zero
je exitLoop

sub cl, 30h //subtracts the hex value of ascii zero
mul ten
add eax, ecx
inc ebx //increments so that it points to the next hex value
jmp conversionLoop

exitLoop:
mov return_val, eax

// at some point in here, you will complete the conversion and
// you will then need to move that value into return_val
}

return return_val; // return_val gets put into eax by the compiler
// so note that when stringToDecimal returns to the calling function, the return value is in
// eax.


}

void strInBase10ToStrInBaseX( char* in_str, char* out_str, short base )
{
int numInBase10; //use this to store the value returned from
//stringToDecimal

__asm
{
push in_str
call stringToDecimal
pop in_str

mov numInBase10, eax;
mov cx, base

baseLoop:

mov dx,0
div cx
push dx
add dx, 48

cmp ax, 0
je exitBaseLoop

mov ax, dx
jmp baseLoop

exitBaseLoop:
pop dx

}
}
 
What you're in the last part is pretty much useless. Since you're desparate, I'll help you out 🙂

I created another function that will accept the output string address, the number to convert (decimal), and the base:

void IntToString(char *out_str, int number, short base)

Here is the code:

void IntToString(char *out_str, int number, short base)
{
_asm
{
mov eax, number;

mov ecx, 0; /* This is our counter - stores length of string */

mov ebx, 0;
mov bx, base;

mov edi, out_str; /* Stores the string's address */

ConvertLoop:
mov edx, 0 /* Prepare for division */
div ebx; /* Quot in EAX, remainder in EDX */
push edx; /* Save remainder - we need this */
inc ecx; /* Increase length */

/* New number is already in EAX */
cmp EAX, 0; /* Finished? */
jle Done;
jmp ConvertLoop

Done:
/* Now, we pop off all the numbers and put them in out_str */
pop eax;
cmp al, 9;
jg AdjustASCII

add al, byte ptr '0'; /* Make ASCII */
jmp StoreNumber

AdjustASCII:
sub al, 10;
add al, byte ptr 'A';

StoreNumber:
mov [edi], al;
inc edi;
loop Done;

/* Null Terminate */
mov [edi], byte ptr '0';

/* All done */
}
}

In the ASCII table, there are a few more letters after 0-9 that we don't need, so we adjust for them by skipping over them, and going directly to the 'A'.

To use the above function, simply get the decimal value from the func. that you wrote, and call this function with the rest of the parameters. Try it!

-Hope that helps 🙂
 
Thanks a lot. That'll help explain a lot. I think I'm not understanding pop/push/and memory loations... or just the whole thing. =P But whats byte ptr? I don't think we learned that and is there a way not to use it?
 
Back
Top