7 Replies - 12245 Views - Last Post: 15 March 2013 - 09:53 PM

#1 tadhg_og   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Array to string in MASM x86

Posted 15 March 2013 - 08:35 PM

Hello,
I have a question regarding converting an array in assembly language to a string and how to load a string of digits or an array of digits into the EAX register without adding them together. I am fairly new at assembly and this is homework so i am not looking for an answer but just some guidance/insight. What i have to do is take user inputted digits as a string and then change the string into numeric form and put it in the EAX register. This is in a macro before the data segment that i will used in a larger program. I have been searching all over the place and have found some resources, such as using atoi, but i am only allowed to use the irvine 32 library. My code so far is posted below. I appreciate any information you may have. Thank you.

    
 
getString		Macro
jmp L2
L0:
	mov		edx, OFFSET prompt_0  ; jump to here if input is not a digit
	call	        writeString
	call	        crlf
 
L2:
mov			edx, OFFSET intro_2  ; start here 
call		        writeString
mov			edx, OFFSET userNumber
mov			ecx, 32
call		        readString
mov 		        esi, OFFSET userNumber
call		        StrLength
mov			ecx, eax		; set loop count

L3:
lodsb			        ; take first byte and load it into eax, also tried lodsw & lodsd but received very large
                                ; numbers in eax after this and wasn't able to convert them to ascii values
call		isDigit		; see if it is a digit
jnz	        L0
sub		eax, 48		; ascii value
push		eax
loop		L3

mov		edx, OFFSET userNumber
call		StrLength
mov		ecx, eax
mov		esi ,OFFSET arrayval

L4:
pop		eax
mov	       [esi], eax
add             esi, 4
loop		L4


mov		  eax, [esi]  ; I know that this is not correct but this is where i am stuck, have tried several things 
mov            String_1,[esi] ; but no luck. I can load them in an array but not in a string without added all the 
                              ; numbers together. So if some inputs 345 i could get 12 but not 345 in eax
ENDM

.data

intro_1		BYTE	"Please provide 10 unsigned decimal integers.", 0Dh, 0Ah, "Each number needs to be small enough to fit inside a 32 bit register.", 0Dh, 0Ah,	
"After you have finished inputting the numbers I will display a list ", 0Dh, 0Ah, "of the integers, their sum and their average value.", 0
intro_2		BYTE	 "Please enter an unsigned number: ", 0
prompt_0	BYTE	"ERROR: You did not enter an unsigned number or your number was too big.", 0
prompt_1	BYTE	"Please try again, Enter unsigned number: ", 0
userNumber	DWORD	33	DUP(0)	;string to be entered by user
arrayVal	DWORD	10	DUP(?)
String_1	dword	33  DUP (0)
.code




Is This A Good Question/Topic? 0
  • +

Replies To: Array to string in MASM x86

#2 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Array to string in MASM x86

Posted 15 March 2013 - 08:49 PM

First, let's move this on over to the Assembly forum....
Was This Post Helpful? 0
  • +
  • -

#3 tadhg_og   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:02 PM

View PostGunnerInc, on 15 March 2013 - 08:49 PM, said:

First, let's move this on over to the Assembly forum....

Sorry I thought i had it in there
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:09 PM

Why read the number as as string THEN convert to a number? Just use ReadInt or ReadDec, which will take the entered ASCII number and convert it to an actual number?

First, you are defining an array that will hold 11 numbers:
arrayVal	DWORD	10	DUP(?)


Indexes start at 0, so to create an array that will hold 10 numbers that should be:
arrayVal	DWORD	9	DUP(?)


Use ReadInt or ReadDec, then add the digit to your array, and repeat 9 times.

Something like this should do it.
    xor     ecx, ecx
    mov     edi, offset arrayVal
GetNextNum:
    call    ReadInt
    mov     dword ptr[edi + ecx * 4], eax
    inc     ecx
    cmp     ecx, Array_Len
    jng     GetNextNum
    
    call    Crlf

    xor     ecx, ecx
    mov     edi, offset arrayVal    
PrintNextNum:
    mov eax, dword ptr[edi + ecx * 4]
    call    WriteDec
    call    Crlf
    inc     ecx
    cmp     ecx, Array_Len
    jng     PrintNextNum
    call    WaitMsg
    exit

Was This Post Helpful? 0
  • +
  • -

#5 tadhg_og   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:19 PM

I know gunner that would make much more sense and i would do that but for the assignment we have to read it as a string.
I have been looking through the irvine library but do not see any direct call for string to int.

This post has been edited by GunnerInc: 15 March 2013 - 09:23 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:30 PM

You don't need to quote the whole post above your reply.

Since you cannot use any conversion routines, you need to do it on your own. First thing you need to do is get acquainted with an ASCII table.

Look at the left column, and under dec you will see the numbers 0 - 9. Now look at the middle column, and under char, look for 0 - 9... Now look under the dec column, and you will see 0 = 48, 1 = 49 ... 9 = 57, these are the ASCII values for the numbers you see on the screen. THIS is what you need to convert to a number.

So, take the number 1, its values is, well, 1! Now, look for the char "1" and its value is 49, what is the difference between 49 and 1??? 48. 48 is the magic number. Use ReadString, check if it is a digit, then subtract 48, and that will give you the number.
Was This Post Helpful? 0
  • +
  • -

#7 tadhg_og   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:46 PM

ok i think i have that part down Gunner, if someone were to say enter 350 and i use readstring and i then move the value over to the eax register either with a lodsb and do it byte by byte or with lodsd and load it as a dword would i then be able to take that value and change to ascii and put it in a string or the eax register as the user wrote it. i have been able to do it by adding up all the values, 350 and add them together to put 8 in eax, but i haven't been able to put them in as the original number. any ideas? i can loop through them but haven't been able to put them in a string or the the register without adding them together.
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Array to string in MASM x86

Posted 15 March 2013 - 09:53 PM

ReadString takes the inputed string and puts the characters in a buffer. Loop through the buffer, byte by byte. Each byte, check if it is a digit, if it is, subtract 48, OH hell, just look at the code for WriteDec which calls AsciiDigit to see how it converts to a number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1