[MASM32, irvine32 library]Converting BYTE string separated by spaces to integer, am I doing it right? - string

Input is random number of integers(does not exceed 1 byte), separated with spaces like this : -3 4 5 7 8 -1
There is ReadInt function in Irvine32 library, as far as I know, one integer as its input function. But since there is no clear information about the number of inputs, I had to declare the space in BYTE string form like this :
keyListInString BYTE 31 DUP(0)
and get integer string like this :
mov edx,OFFSET keyListInString
mov ecx,SIZEOF keyListInString
call ReadString
and I tried to separate each integers by recognizing spaces,'-' symbol and manually inserting them to new space I have created.
keyListInInteger BYTE 31 DUP(0) ;new space for integers
And here is the code I used.
mov esi,OFFSET keyListInString
mov edi,OFFSET keyListInInteger
mov ecx,eax ;eax = length of string = number of cycles
L1:
mov al,[esi]
cmp al,45 ;if current component of keyListInString is '-'
je ifMinus
mov al,[esi]
cmp al,0 ;if this is null(space)
je ifNull
mov al,[esi] ;if its none of above(normal number)
sub al,48 ;sub 48 for ascii --> integer
mov [edi],al ;put integer to where edi points(keyListInInteger)
inc edi
jmp final
ifMinus:
inc esi ;points next index on keyListInString(number part)
mov al,[esi]
sub al,48
neg al ;have to reverse the sign, because it is originally a minus
mov [edi],al
inc edi ;manually increase edi
jmp final
ifNull:
inc esi ;it is space anyway, so point next.
jmp L1 ;and repeat from first.
final:
inc esi
loop L1
I tried to check if this is the right code in this way :
mov edx,OFFSET keyListInInteger
mov al,[edx] ;see [edx+2],[edx+4],...
movzx eax,al
call DumpRegs
and see al's value.
I think I somehow approached to the result I wanted, but not exactly.
I wanted to make each integers like elements in an array, but I have no idea how to do so.

Related

converting string IP address to 32-bit integer (ASM)

I'm trying to figure out how to take a null-terminated normal IP address as a string and convert it to a 32-bit integer. In this example, my string is located at address ES:DI+Qapktdat. Qapktdat is a member of a struct defined as a string stored at ES:DI.
I have no problem reading from or writing to strings in the struct, but I have a problem with calculating the IP.
For example, If I use the string "192.168.7.2" then EDX should equal Hex value C0A80702 but I'm getting nothing close to it.
What could I be doing wrong?
ipconvmath:
mov BX,Qapktdat-1 ;BX=data pointer. Set it to beginning of data -1
mov CX,11h ;Look for null within the first 17 bytes of data
ipend:
inc BX
mov AL,[ES:DI+BX]
cmp AL,0
je ipok
loop ipend
mov DX,-1 ;cant find null char so exit
ret
ipok:
mov BP,10 ;BP = multiplier of multiplier
xor EDX,EDX ;EDX = eventual IP address
;We scan IP address starting at LAST digit
nextipseg:
mov CX,1 ;CX= immediate multiplier
xor DL,DL ;lower 8 bits of EDX = 0
nxb:
xor AX,AX
dec BX
mov AL,[ES:DI+BX]
cmp AL,'.'
je nodot
;Input isnt DOT so see if its number 0-9
cmp AL,30h ;#0
jb badipcnv
cmp AL,39h ;#9
ja badipcnv
sub AL,30h
mul CX ;multiply by 1, 10, or 100 depending on number position
add DL,AL ;add it to our numeric IP
mov AX,CX ;temporarily make AX=CX so multiply can work
mul BP ;multiply the immediate multiplier by 10
mov CX,AX
cmp BX,Qapktdat ;see if were at beginning of data
jne nxb
;we are so skip beginning
nodot:
rol EDX,8 ;shift existing contents over by 1 byte
cmp BX,Qapktdat ;see if were at beginning
jne nextipseg
;here we are at beginning so were done
;EDX = ip address converted
ret
;Reach here if a character is not 0-9 or a dot
badipcnv:
mov AX,1
ret

Reversing a string given by the user in Assembly Language

I am working on a program in Assembly Language right now where the user can input any word of their preference and then the program will reverse it. So, if the user inputs HELLO and the program will output OLLEH.
The algorithm I decided to implement was by first getting the length of the string and storing it in length. I will then use the length to traverse backward in the given string and store each character in a new memory location which is REVERSE. I tested my program several times but it only outputs one character. I tried several words and noticed that the character is always output is the second character of the string so I am unsure if it is able to reverse the word or not.
Did I miss anything or I implemented a code block incorrectly?
Note: The GET_STRING[end] 5 is used when I run the code in .exe format.
Below is the code I am currently working on:
%include "io.inc"
section .data
string dd 0x00
end dd 0x00
REVERSE dd 0x00
length db 0
section .text
global CMAIN
CMAIN:
;write your code here
PRINT_STRING "Please enter a string: "
GET_STRING [string], 10
lea esi, [string]
lea edi, [REVERSE]
lea ecx, [length]
L1:
mov al, [esi]
cmp al, 0
JE FINISH
JNE INCREMENT
inc esi
jmp L1
INCREMENT:
inc byte[length]
inc esi
jmp L1
FINISH:
L2:
;points to the current index of the string (i.e. if Hello,it will first point at 'o' which is 5
mov al, [ecx]
cmp cl, 0 ;checks if length == 0
JE DONE
JNE DECREMENT
dec ecx
jmp L2
DECREMENT:
mov byte[edi], al ;adds a character from [string]
dec ecx
jmp L2
DONE:
PRINT_STRING REVERSE
GET_STRING[end], 5
xor eax, eax
ret
If GET_STRING gives you a zero-terminated string then your L1 part will find the length of that string.
This is what remains after removing the redundant code:
lea esi, [string]
L1:
mov al, [esi]
cmp al, 0
je FINISH
inc byte [length]
inc esi
jmp L1
FINISH:
lea ecx, [length]
In the L2 part however
you are using the address of the length variable as if it were an address into the string itself!
you are checking for length == 0 from a register that you did not load with the length
At the start of part L2, the ESI register still points at the terminating zero. So you'll have to decrement first and then fetch and store the character.
Of course you can't copy any characters if the length variable contains 0. So check this condition first.
lea edi, [REVERSE]
mov cl, [length]
test cl, cl
jz DONE
L2:
dec esi ; Decrement
mov al, [esi] ; Fetch
mov [edi], al ; Store
inc edi
dec cl ; 1 more character done
jnz L2
DONE:
mov [edi], cl ; CL=0 at this point, now zero-terminating the result
If you keep your current data definitions, then don't input more than 3 characters. A dword dd allows for just 3 characters and 1 terminating zero.
Alternatively widen your buffers:
string db 11 dup (0) ; GET_STRING [string], 10
end db 6 dup (0) ; GET_STRING [end], 5
REVERSE db 11 dup (0)
length db 0
Below is a version that doesn't use a memory-based length variable. The length of the string is stored in ECX.
lea edi, [REVERSE]
lea esi, [string]
L1:
mov al, [esi]
inc esi
cmp al, 0
jne L1
lea ecx, [esi - 1 - String]
jecxz DONE
; ESI points behind the terminating zero, the fetch uses a -2 offset (avoiding address stall)
L2:
mov al, [esi - 2] ; Fetch
mov [edi], al ; Store
dec esi
inc edi
dec ecx ; 1 more character done
jnz L2
DONE:
mov [edi], cl ; CL=0 at this point, now zero-terminating the result

How to find the hamming distance for strings that are not necessarily equal length?

I have an assignment asking me to find the hamming distance of two user-input strings that are not necessarily equal in length.
So, I made the following algorithm:
Read both strings
check the length of each string
compare the length of the strings
if(str1 is shorter)
set counter to be the length of str1
END IF
if(str1 is longer)
set counter to be the length of str2
END IF
if(str1 == str2)
set counter to be length of str1
END IF
loop through each digit of the strings
if(str1[digitNo] XOR str2[digitNo] == 1)
inc al
END IF
the final al value is the hamming distance of the strings, print it.
But I'm stuck at step 3 and I don't seem to get it working. any help?
I tried playing around with the registers to save the values in, but none of that worked, I still didn't get it working.
; THIS IS THE CODE I GOT
.model small
.data
str1 db 255
db ?
db 255 dup(?)
msg1 db 13,10,"Enter first string:$"
str2 db 255
db ?
db 255 dup(?)
msg2 db 13,10,"Enter second string:$"
one db "1"
count db ?
.code
.startup
mov ax,#data
mov ds,ax
; printing first message
mov ah, 9
mov dx, offset msg1
int 21h
; reading first string
mov ah, 10
mov dx, offset str1
int 21h
; printing second message
mov ah, 9
mov dx, offset msg2
int 21h
; reading second string
mov ah, 10
mov dx, offset str2
int 21h
; setting the values of the registers to zero
mov si, 0
mov di, 0
mov cx, 0
mov bx, 0
; checking the length of the first string
mov bl, str1+1
add bl, 30h
mov ah, 02h
mov dl, bl
int 21h
; checking the length of the second string
mov bl, str2+1
add bl, 30h
mov ah, 02h
mov dh, bl
int 21h
; comparing the length of the strings
cmp dl,dh
je equal
jg str1Greater
jl str1NotGreater
; if the strings are equal we jump here
equal:
mov cl, dl
call theLoop
; if the first string is greater than the second, we jump here and set counter of str1
str1Greater:
; if the second string is greater than the first, we jump here and set counter to length of str2
Str1NotGreater:
; this is the loop that finds and prints the hamming distance
;we find it by looping over the strings and taking the xor for each 2, then incrementing counter of ones for each xor == 1
theLoop:
end
So, in the code I provided, it's supposed to print the length of each string (it prints the lengths next to each other), but it seems to always keep printing the length of the first string, twice. The register used to store the length of the first string is dl, and the register used to store the length of the second is dh, if I change it back to dl, it would then print the correct length, but I want to compare the lengths, and I think it won't be possible to do so if I save it in dl both times.
but it seems to always keep printing the length of the first string, twice.
When outputting a character with the DOS function 02h you don't get to choose which register to use to supply the character! It's always DL.
Since after printing both lengths you still want to work with these lengths it will be better to not destroy them in the first place. Put the 1st length in BL and the second length in BH. For outputting you copy these in turn to DL where you do the conversion to a character. This of course can only work for strings of at most 9 characters.
; checking the length of the first string
mov BL, str1+1
mov dl, BL
add dl, 30h
mov ah, 02h
int 21h
; checking the length of the second string
mov BH, str2+1
mov dl, BH
add dl, 30h
mov ah, 02h
int 21h
; comparing the length of the strings
cmp BL, BH
ja str1LONGER
jb str1SHORTER
; if the strings are equal we ** FALL THROUGH ** here
equal:
mov cl, BL
mov ch, 0
call theLoop
!!!! You need some way out at this point. Don't fall through here !!!!
; if the first string is greater than the second, we set counter of str1
str1LONGER:
; if the second string is greater than the first, we set counter to length of str2
Str1SHORTER:
; this is the loop that finds and prints the hamming distance
;we find it by looping over the strings and taking the xor for each 2, then incrementing counter of ones for each xor == 1
theLoop:
Additional notes
Lengths are unsigned numbers. Use the unsigned conditions above and below.
Talking about longer and shorter makes more sense for strings.
Don't use 3 jumps if a mere fall through in the code can do the job.
Your code in theLoop will probably use CX as a counter. Don't forget to zero CH. Either using 2 instructions like I did above or else use movzx cx, BL if you're allowed to use instructions that surpass the original 8086.
Bonus
mov si, offset str1+2
mov di, offset str2+2
mov al, 0
MORE:
mov dl, [si]
cmp dl, [di]
je EQ
inc al
EQ:
inc si
inc di
loop MORE

Assembly x86 Date to Number - Breaking a string into smaller sections

I'm actually looking to be pointed in the right direction on an issue.
I'm looking to convert a date in x86 Assembly from the format "DD-MMM-YYYY" to a unique number so that it can be bubble sorted later and eventually converted back.
So, when I have a string input ie:
.data
inDate dw "08-SEP-1993"
And I want to split it up to
day = "08"
month = "SEP"
year = "1993"
So that I can process it further (I'll be converting SEP to "7", ect.)
So my question is what is a simple, efficient way to break the date down (code-wise)? I know I'll need to convert the date format to allow for sorting, but I'm new to Assembly so I'm not positive how to break the string up so I can convert it.
Also, as a second question, how would you convert a number from the string to an actual numerical value?
Thanks!
NOTE: I suppose it should be noted I'm using masm32
Next little program was made with EMU8086 (16 bits), it captures numbers from keyboard as strings, convert them to numeric to compare, and finally it converts a number to string to display. Notice the numbers are captured with 0AH, which requieres a 3-level variable "str". The conversion procedures that you need are at the bottom of the code (string2number and number2string).
.model small
.stack 100h
.data
counter dw ?
msj1 db 'Enter a number: $'
msj2 db 'The highest number is: $'
break db 13,10,'$'
str db 6 ;MAX NUMBER OF CHARACTERS ALLOWED (4).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 6 dup (?) ;CHARACTERS ENTERED BY USER.
highest dw 0
buffer db 6 dup(?)
.code
;INITIALIZE DATA SEGMENT.
mov ax, #data
mov ds, ax
;-----------------------------------------
;CAPTURE 5 NUMBERS AND DETERMINE THE HIGHEST.
mov counter, 5 ;HOW MANY NUMBERS TO CAPTURE.
enter_numbers:
;DISPLAY MESSAGE.
mov dx, offset msj1
call printf
;CAPTURE NUMBER AS STRING.
mov dx, offset str
call scanf
;DISPLAY LINE BREAK.
mov dx, offset break
call printf
;CONVERT CAPTURED NUMBER FROM STRING TO NUMERIC.
mov si, offset str ;PARAMETER (STRING TO CONVERT).
call string2number ;NUMBER RETURNS IN BX.
;CHECK IF CAPTURED NUMBER IS THE HIGHEST.
cmp highest, bx
jae ignore ;IF (HIGHEST >= BX) IGNORE NUMBER.
;IF NO JUMP TO "IGNORE", CURRENT NUMBER IS HIGHER THAN "HIGHEST".
mov highest, bx ;CURRENT NUMBER IS THE HIGHEST.
ignore:
;CHECK IF WE HAVE CAPTURED 5 NUMBERS ALREADY.
dec counter
jnz enter_numbers
;-----------------------------------------
;DISPLAY HIGHEST NUMBER.
;FIRST, FILL BUFFER WITH '$' (NECESSARY TO DISPLAY).
mov si, offset buffer
call dollars
;SECOND, CONVERT HIGHEST NUMBER TO STRING.
mov ax, highest
mov si, offset buffer
call number2string
;THIRD, DISPLAY STRING.
mov dx, offset msj2
call printf
mov dx, offset buffer
call printf
;FINISH PROGRAM.
mov ax, 4c00h
int 21h
;-----------------------------------------
;PARAMETER : DX POINTING TO '$' FINISHED STRING.
proc printf
mov ah, 9
int 21h
ret
endp
;-----------------------------------------
;PARAMETER : DX POINTING TO BUFFER TO STORE STRING.
proc scanf
mov ah, 0Ah
int 21h
ret
endp
;------------------------------------------
;CONVERT STRING TO NUMBER.
;PARAMETER : SI POINTING TO CAPTURED STRING.
;RETURN : NUMBER IN BX.
proc string2number
;MAKE SI TO POINT TO THE LEAST SIGNIFICANT DIGIT.
inc si ;POINTS TO THE NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;NUMBER OF CHARACTERS ENTERED.
mov ch, 0 ;CLEAR CH, NOW CX==CL.
add si, cx ;NOW SI POINTS TO LEAST SIGNIFICANT DIGIT.
;CONVERT STRING.
mov bx, 0
mov bp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
repeat:
;CONVERT CHARACTER.
mov al, [ si ] ;CHARACTER TO PROCESS.
sub al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
mov ah, 0 ;CLEAR AH, NOW AX==AL.
mul bp ;AX*BP = DX:AX.
add bx, ax ;ADD RESULT TO BX.
;INCREASE MULTIPLE OF 10 (1, 10, 100...).
mov ax, bp
mov bp, 10
mul bp ;AX*10 = DX:AX.
mov bp, ax ;NEW MULTIPLE OF 10.
;CHECK IF WE HAVE FINISHED.
dec si ;NEXT DIGIT TO PROCESS.
loop repeat ;COUNTER CX-1, IF NOT ZERO, REPEAT.
ret
endp
;------------------------------------------
;FILLS VARIABLE WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THE STRING WILL BE DISPLAYED.
;PARAMETER : SI = POINTING TO STRING TO FILL.
proc dollars
mov cx, 6
six_dollars:
mov bl, '$'
mov [ si ], bl
inc si
loop six_dollars
ret
endp
;------------------------------------------
;CONVERT A NUMBER IN STRING.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING (STR).
;PARAMETERS : AX = NUMBER TO CONVERT.
; SI = POINTING WHERE TO STORE STRING.
proc number2string
mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov dx, 0 ;NECESSARY TO DIVIDE BY BX.
div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp ax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
endp
Now the 32 bits version. Next is a little program that assigns to EAX a big number, convert it to string and convert it back to numeric, here it is:
.model small
.586
.stack 100h
.data
msj1 db 13,10,'Original EAX = $'
msj2 db 13,10,'Flipped EAX = $'
msj3 db 13,10,'New EAX = $'
buf db 11
db ?
db 11 dup (?)
.code
start:
;INITIALIZE DATA SEGMENT.
mov ax, #data
mov ds, ax
;CONVERT EAX TO STRING TO DISPLAY IT.
call dollars ;NECESSARY TO DISPLAY.
mov eax, 1234567890
call number2string ;PARAMETER:AX. RETURN:VARIABLE BUF.
;DISPLAY 'ORIGINAL EAX'.
mov ah, 9
mov dx, offset msj1
int 21h
;DISPLAY BUF (EAX CONVERTED TO STRING).
mov ah, 9
mov dx, offset buf
int 21h
;FLIP EAX.
call dollars ;NECESSARY TO DISPLAY.
mov eax, 1234567890
call flip_eax ;PARAMETER:AX. RETURN:VARIABLE BUF.
;DISPLAY 'FLIPPED EAX'.
mov ah, 9
mov dx, offset msj2
int 21h
;DISPLAY BUF (EAX FLIPPED CONVERTED TO STRING).
mov ah, 9
mov dx, offset buf
int 21h
;CONVERT STRING TO NUMBER (FLIPPED EAX TO EAX).
mov si, offset buf ;STRING TO REVERSE.
call string2number ;RETURN IN EBX.
mov eax, ebx ;THIS IS THE NEW EAX FLIPPED.
;CONVERT EAX TO STRING TO DISPLAY IT.
call dollars ;NECESSARY TO DISPLAY.
call number2string ;PARAMETER:EAX. RETURN:VARIABLE BUF.
;DISPLAY 'NEW EAX'.
mov ah, 9
mov dx, offset msj3
int 21h
;DISPLAY BUF (EAX CONVERTED TO STRING).
mov ah, 9
mov dx, offset buf
int 21h
;WAIT UNTIL USER PRESS ANY KEY.
mov ah, 7
int 21h
;FINISH PROGRAM.
mov ax, 4c00h
int 21h
;------------------------------------------
flip_eax proc
mov si, offset buf ;DIGITS WILL BE STORED IN BUF.
mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
extracting:
;EXTRACT ONE DIGIT.
mov edx, 0 ;NECESSARY TO DIVIDE BY EBX.
div ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
;INSERT DIGIT IN STRING.
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
;NEXT DIGIT.
cmp eax, 0 ;IF NUMBER IS
jne extracting ;NOT ZERO, REPEAT.
ret
flip_eax endp
;------------------------------------------
;CONVERT STRING TO NUMBER IN EBX.
;SI MUST ENTER POINTING TO THE STRING.
string2number proc
;COUNT DIGITS IN STRING.
mov cx, 0
find_dollar:
inc cx ;DIGIT COUNTER.
inc si ;NEXT CHARACTER.
mov bl, [ si ]
cmp bl, '$'
jne find_dollar ;IF BL != '$' JUMP.
dec si ;BECAUSE IT WAS OVER '$', NOT OVER THE LAST DIGIT.
;CONVERT STRING.
mov ebx, 0
mov ebp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
repeat:
;CONVERT CHARACTER.
mov eax, 0 ;NOW EAX==AL.
mov al, [ si ] ;CHARACTER TO PROCESS.
sub al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
mul ebp ;EAX*EBP = EDX:EAX.
add ebx, eax ;ADD RESULT TO BX.
;INCREASE MULTIPLE OF 10 (1, 10, 100...).
mov eax, ebp
mov ebp, 10
mul ebp ;AX*10 = EDX:EAX.
mov ebp, eax ;NEW MULTIPLE OF 10.
;CHECK IF WE HAVE FINISHED.
dec si ;NEXT DIGIT TO PROCESS.
loop repeat ;CX-1, IF NOT ZERO, REPEAT.
ret
string2number endp
;------------------------------------------
;FILLS VARIABLE STR WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THE STRING WILL BE DISPLAYED.
dollars proc
mov si, offset buf
mov cx, 11
six_dollars:
mov bl, '$'
mov [ si ], bl
inc si
loop six_dollars
ret
dollars endp
;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN EAX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING (BUF).
number2string proc
mov ebx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov edx, 0 ;NECESSARY TO DIVIDE BY EBX.
div ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED (DL) FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp eax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
mov si, offset buf
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
number2string endp
end start

How to convert a DWORD into a DB

I want to display a score in a game that I built in MASM32, and I have a problem, how do I convert a DWORD to a DB (string).
There is the function crt__itoa to convert a dword to an integer , but for some reason it doesn't work (do i need to include an other lib ? ).
There is the function TextOutA to display a score, but again I cant print it out because I don't have a string so it can print it from.
do i need to include an other lib? - Probably. You need msvcrt.inc and msvcrt.lib for crt__itoa.
masm32rt.inc is the Swiss Army Knife for such cases. Here is a working example:
include c:\masm32\include\masm32rt.inc
.DATA
fmt db "%s",10,0
num dd 1234567
num_str db 16 dup (?)
.CODE
main PROC
; with CALL:
push 10
push OFFSET num_str
push num
call crt__itoa
add esp, 12
push OFFSET num_str
push OFFSET fmt
call crt_printf
add esp, 8
; or with INVOKE:
invoke crt__itoa, num, OFFSET num_str, 10
invoke crt_printf, OFFSET fmt, OFFSET num_str
invoke ExitProcess, 0
main ENDP
END main
The program does not stop. If you don't call it in an extra command prompt window it will open a window and close it immediately. In Qeditor I suggest to insert a line "Run & Pause" into menus.ini:
...
&Run Program,"{b}.exe"
Run && Pause,cmd.exe /C"{b}.exe" & pause
...
Now you have a new item under "Project".
Next is a method made with Visual Studio 2010 C++ that manually converts EAX into a string (it doesn't need any library, just copy-paste and use). It takes a number as parameter, assign it to EAX, convert it to string and display the string :
void number2string ( int value ) {
char buf[11];
__asm { ;EXTRACT DIGITS ONE BY ONE AND PUSH THEM INTO STACK.
mov eax, value
mov ebx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov edx, 0 ;NECESSARY TO DIVIDE BY EBX.
div ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED (DL) FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp eax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS IN REVERSE ORDER.
mov esi, 0 ;POINTER TO STRING'S CHARACTERS.
cycle2:
pop dx ;GET A DIGIT.
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov buf[ esi ], dl
inc esi ;NEXT POSITION IN STRING.
loop cycle2
mov buf[ esi ], 0 ;MAKE IT ASCIIZ STRING.
}
printf( buf );
scanf( "%s",buf ); // TO STOP PROGRAM AND LET US SEE RESULT.
}
Pay attention : previous method is a "void", so you call it as usual :
number2string( 1234567890 ); // CONVERT THIS BIG NUMBER IN STRING AND DISPLAY.
You can modify the method to return the string or to do anything you want.
Now (for those who are tough enough) the same previous procedure for pure assembler, made with GUI Turbo Assembler x64 (http://sourceforge.net/projects/guitasm8086/), this full program shows how it works :
.model small
.586
.stack 100h
.data
buf db 11 dup (?) ;STRING.
.code
start:
;INITIALIZE DATA SEGMENT.
mov ax, #data
mov ds, ax
;CONVERT EAX TO STRING.
call dollars ;FILL BUF WITH '$', NECESSARY TO DISPLAY.
mov eax, 1234567890
call number2string ;PARAMETER:EAX. RETURN:VARIABLE BUF.
;DISPLAY BUF (EAX CONVERTED TO STRING).
mov ah, 9
mov dx, offset buf
int 21h
;WAIT UNTIL USER PRESS ANY KEY.
mov ah, 7
int 21h
;FINISH PROGRAM.
mov ax, 4c00h
int 21h
;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN EAX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING (BUF).
number2string proc
mov ebx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov edx, 0 ;NECESSARY TO DIVIDE BY EBX.
div ebx ;EDX:EAX / 10 = EAX:QUOTIENT EDX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED (DL) FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp eax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
mov si, offset buf
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
number2string endp
;------------------------------------------
;FILLS VARIABLE BUF WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THE STRING WILL BE DISPLAYED.
dollars proc
mov si, offset buf
mov cx, 11
six_dollars:
mov bl, '$'
mov [ si ], bl
inc si
loop six_dollars
ret
dollars endp
end start

Resources