Hello, im trying to create a program with MASM where the program asks me to enter a few numbers. So lets say i enter the numbers '12345', then it should calculate the sum of those numbers which would result in '15' as a answer.
The only problem is that i dont know how to sepperate the whole decimal striing 12345 into sepperate numbers. I still haven't figured it out.
I have this code right now, with a few comments in it. Im hoping that someone can help me out with this.
The only problem is that i dont know how to sepperate the whole decimal striing 12345 into sepperate numbers. I still haven't figured it out.
I have this code right now, with a few comments in it. Im hoping that someone can help me out with this.
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
.data
;nummers dd 1,2,3,4,5 ;**** Normally i would've used this one
;nummers dd 0 ;But now im creating the 'array' without hardcoding their values in it.
message db "Totaal is %d",0
.data?
buffer db 100 dup(?)
nummers db 250 dup(?) ;Tried this as dd and db
.code
start:
xor eax, eax
mov ecx, 5
invoke StdIn, ADDR nummers, 250 ;i enter: 12345 in console
sub eax, 2
mov BYTE PTR ,0 ;strip off ,10,13 'CR+LF'
invoke atodw, nummers ;convert to dword
mov edx, OFFSET nummers
Count:
adc eax, DWORD PTR
add edx, 4
dec ecx
jnz Count
invoke wsprintf,ADDR buffer, ADDR message, eax
invoke StdOut, ADDR buffer
invoke ExitProcess, 0
END start
The only problem is that i dont know how to sepperate the whole decimal striing 12345 into sepperate numbers
means 1,2,3,4,5 is it then method is simple
12345 mod 10 =5
push 5
divide 12345/10=1234
1234 mod 10=4
push 4
divide 1234/10=123
123 mod 10=3
push 3
divide 123/10=12
12 mod 10=2
push 2
divide 12/10=1
1 mod 10 =1
push 1
pop 1
pop 2
pop 3
pop 4
pop 5
means 1,2,3,4,5 is it then method is simple
12345 mod 10 =5
push 5
divide 12345/10=1234
1234 mod 10=4
push 4
divide 1234/10=123
123 mod 10=3
push 3
divide 123/10=12
12 mod 10=2
push 2
divide 12/10=1
1 mod 10 =1
push 1
pop 1
pop 2
pop 3
pop 4
pop 5
Thanks, but i dont think thats gonna work.
I used 12345 as an example, but what if a user enters 2535 as a number.
I could wrong about your example, its just that i dont see how i can make this work with any other value.
I used 12345 as an example, but what if a user enters 2535 as a number.
I could wrong about your example, its just that i dont see how i can make this work with any other value.
2535
2535 mod 10 =5 push 5 and 2535/10=253
253 mod 10=3 push 3 and 253/10 =25
25 mod 10 =5 push 5 and 25/10=2
2 mod 10=2 push 2
pop 2
pop 5
pop 3
pop 5
any value,have u try
2535 mod 10 =5 push 5 and 2535/10=253
253 mod 10=3 push 3 and 253/10 =25
25 mod 10 =5 push 5 and 25/10=2
2 mod 10=2 push 2
pop 2
pop 5
pop 3
pop 5
any value,have u try
Let's say the EAX register holds the value of 0x00000010 which is equal to 16 in decimal. Now if you divide this value by 10 one time, the EAX will be set to 0x00000001 as the quotient and the EDX register will carry 0x00000006 as the remainder. Now we must keep the remainders until the number in the EAX register is zero. Therefore:
EAX = 0x00000010 / 0x0000000A => Remainder(EDX) = 0x00000006, Quotient(EAX) = 0x00000001
EAX != 0x00000000 so keep on dividing
EAX = 0x00000001 / 0x0000000A => Remainder(EDX) = 0x00000001, Quotient(EAX) = 0x00000000
Now that EAX is zero, you can reverse the order of the values in the EDX from down to up to get 0x00000001 and 0x00000006 which if put after each other would be 1 and 6 or 16.
EAX = 0x00000010 / 0x0000000A => Remainder(EDX) = 0x00000006, Quotient(EAX) = 0x00000001
EAX != 0x00000000 so keep on dividing
EAX = 0x00000001 / 0x0000000A => Remainder(EDX) = 0x00000001, Quotient(EAX) = 0x00000000
Now that EAX is zero, you can reverse the order of the values in the EDX from down to up to get 0x00000001 and 0x00000006 which if put after each other would be 1 and 6 or 16.
Thanks for the explanation, its a bit more clear now, so im gonna try some things out.
Posted on 2006-10-19 15:51:52 by XCHG
Sorry i forgot all about this but, thanks alot!
I wanted to read the old posts so i could give it a try again, but then i noticed your post. It works excellent!
I was just hoping if you have the time, that you could explain the code. It feels better if i know what im assembling instead of guessing what i could be.
Anyway, thanks again!
I wanted to read the old posts so i could give it a try again, but then i noticed your post. It works excellent!
I was just hoping if you have the time, that you could explain the code. It feels better if i know what im assembling instead of guessing what i could be.
Anyway, thanks again!
This is what we have to do. We have to allocate a console window for our program first because we are going to create a console application. Then we have to get the handle to the console window with the read and the write access so we can read from and write to it.
After having passed the above steps, it is time to tell the user what we are intending to do and wait for his or her answer. When the user hits the Enter Key, we should start our main task by first removing the Carriage Return and the Line Feed from the end of the entered string. If you don?t do that, the answer that you will get will be inaccurate because you will be calculating the integral value of the CR and the LF too.
Now we have to pass this string to a function which tells us if the string contains only numeral values or not. You can do this by masking out the fourth and the fifth bits of individual bytes inside the string. The byte will then be numeral if it contains the value 9 or below, down to zero. For example, if the user has entered ?A90?, you will ?AND? the ASCII value of the character ?A? with 0xCF to mask-out the fourth and the fifth bits. The ASCII value of the character ?A? is 0x41 which when applied the ?AND? instruction with 0xCF will result in 0x41. This is not a value between the range 0..9 so it is not an integral value, thus, we have to return ?False?.
After confirming that the string contains integral values, solely, we have to read each and every byte of the string and convert them to their corresponding integral values using the ?AND 0xCF? again and sum up all these values. I have modified the code for you in my previous post so that it contains comment on each line.
After having passed the above steps, it is time to tell the user what we are intending to do and wait for his or her answer. When the user hits the Enter Key, we should start our main task by first removing the Carriage Return and the Line Feed from the end of the entered string. If you don?t do that, the answer that you will get will be inaccurate because you will be calculating the integral value of the CR and the LF too.
Now we have to pass this string to a function which tells us if the string contains only numeral values or not. You can do this by masking out the fourth and the fifth bits of individual bytes inside the string. The byte will then be numeral if it contains the value 9 or below, down to zero. For example, if the user has entered ?A90?, you will ?AND? the ASCII value of the character ?A? with 0xCF to mask-out the fourth and the fifth bits. The ASCII value of the character ?A? is 0x41 which when applied the ?AND? instruction with 0xCF will result in 0x41. This is not a value between the range 0..9 so it is not an integral value, thus, we have to return ?False?.
After confirming that the string contains integral values, solely, we have to read each and every byte of the string and convert them to their corresponding integral values using the ?AND 0xCF? again and sum up all these values. I have modified the code for you in my previous post so that it contains comment on each line.
Thanks a lot! Its a good explanation, i kinda understand it all now :)