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

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

Related

Finding the substring in an input string

I have this assembly program where I need to find the substring in the main string I input. My problem is that it always outputs the "word found" even if I typed two completely different words. I don't know which part of my loop or condition is wrong. Please help me figure it out. Also, please suggest some string instructions that could be used in checking for a substring so that I can shorten my code. I am really confused with how the cmpsb works, I only tried to use it. Btw, I don't know how to use a debugger that's why I can't debug my code and I am just a newbie in assembly language.
Below is the logic part of my code.
.data
prompt1 db "Input String: $"
prompt2 db 10,10, 13, "Input Word: $"
prompt3 db 10,10, 13, "Output: $"
found db "Word Found. $"
notfound db "Word Not Found. $"
invalid db 10,10, 13, "Invalid. $"
InputString db 21,?,21 dup("$")
InputWord db 21,?,21 dup("$")
actlen db ?
strlen dw ($-InputWord)
.code
start:
mov ax, #data
mov ds, ax
mov es, ax
;Getting input string
mov ah,09h
lea dx, prompt1
int 21h
lea si, InputString
mov ah, 0Ah
mov dx, si
int 21h
;Getting input word
mov ah,09h
lea dx, prompt2
int 21h
lea di, InputWord
mov ah, 0Ah
mov dx, di
int 21h
;To check if the length of substring is shorter than the main string
mov cl, [si+1]
mov ch, 0
add si, cx
mov bl, [di+1]
mov bh, 0
cmp bx, cx
ja invalid_length
je valid
jb matching
valid:
cld
repe cmpsb
je found_display
jne notfound_display
matching:
mov al, [si]
mov ah, [di]
cmp al, ah
je check
jne iterate
iterate:
inc si
mov dx, strlen
dec dx
cmp dx, 0
je notfound_display
jmp matching
check:
mov cl, [di+1]
mov ch, 0
mov ax, si
add ax, 1
cld
repe cmpsb
jne again
jmp found_display
again:
mov si, ax
dec dx
lea di, InputWord
jmp matching
invalid_length:
mov ah, 09h
lea dx, invalid
int 21h
strlen dw ($-InputWord)
This does nothing useful. The length that it calculate can not help you in any way!
;To check if the length of substring is shorter than the main string
mov cl, [si+1]
mov ch, 0
add si, cx
mov bl, [di+1]
mov bh, 0
cmp bx, cx
Here (as Jester told you) the add si, cx instruction is wrong. You need add si, 2 to set SI to the start of the string. You will also need to add add di, 2 to set DI to the start of the word. Do this and the valid part of your program will work correctly.
For the matching part:
Consider the case where the string has 7 characters and the word that you're looking for has 6 characters. You can find the word in at most 2 ways.
Consider the case where the string has 8 characters and the word that you're looking for has 6 characters. You can find the word in at most 3 ways.
Consider the case where the string has 9 characters and the word that you're looking for has 6 characters. You can find the word in at most 4 ways.
Notice the regularity? The number of possible finds is equal to the difference in length plus 1.
mov bp, cx ;CX is length string (long)
sub bp, bx ;BX is length word (short)
inc bp
This sets BP to the number of tries in your matching routine.
cld
lea si, [InputString + 2]
lea di, [InputWord + 2]
matching:
mov al, [si] ;Next character from the string
cmp al, [di] ;Always the first character from the word
je check
continue:
inc si ;DI remains at start of the word
dec bp
jnz matching ;More tries to do
jmp notfound_display
The check part will use repe cmpsb to test for a match, but in the event that the match is not found, you must be able to return to the matching code at the continue label. You have to preserve the registers.
check:
push si
push di
mov cx, bx ;BX is length of word
repe cmpsb
pop di
pop si
jne continue
jmp found_display

How to convert String to Number in 8086 assembly?

I have to build a Base Converter in 8086 assembly .
The user has to choose his based and then put a number,
after then , the program will show him his number in 3 more bases[he bring a decimal number, and after this he will see his number in hex, oct, and bin.
This first question is, how can I convert the number he gave me, from string, to a number?
the sec question is, how can i convert? by RCR, and then adc some variable?
Here is my code:
data segment
N=8
ERROR_STRING_BASE DB ,10,13, " THIS IS NOT A BASE!",10,13, " TRY AGINE" ,10,13," $"
OPENSTRING DB " Welcome, to the Base Convertor",10,13," Please enter your base to convert from:",10,13," <'H'= Hex, 'D'=Dec, 'O'=oct, 'B'=bin>: $"
Hex_string DB "(H)" ,10,13, "$"
Octalic_string DB "(O) ",10,13, "$"
Binar_string DB "(B)",10,13, "$"
Dece_string DB "(D)",10,13, "$"
ENTER_STRING DB ,10,13, " Now, Enter Your Number (Up to 4 digits) ",10,13, "$"
Illegal_Number DB ,10,13, " !!! This number is illegal, lets Start again" ,10,13,"$"
BASED_BUFFER DB N,?,N+1 DUP(0)
Number_buffer db N, ? ,N+1 DUP(0)
TheBase DB N DUP(0)
The_numer DB N DUP(0)
The_binNumber DB 16 DUP(0)
data ends
sseg segment stack
dw 128 dup(0)
sseg ends
code segment
assume ss:sseg,cs:code,ds:data
start: mov ax,data
mov ds,ax
MOV DX,OFFSET OPENSTRING ;PUTS THE OPENING SRTING
MOV AH,9
INT 21H
call EnterBase
CALL CheckBase
HEXBASE: CALL PRINTtheNUMBER
MOV DX,OFFSET Hex_string
MOV AH,9
INT 21h
JMP I_have_the_numberH
oCTALICbASE: CALL PRINTtheNUMBER
MOV DX,OFFSET Octalic_string
MOV AH,9
INT 21h
JMP I_have_the_numberO
BINBASE:CALL PRINTtheNUMBER
MOV DX,OFFSET Binar_string
MOV AH,9
INT 21h
JMP I_have_the_numberB
DECBASE: CALL PRINTtheNUMBER
MOV DX,OFFSET Dece_string
MOV AH,9
INT 21h
JMP I_have_the_numberD
I_have_the_numberH: CALL BINcalculation
CALL OCTcalculation
CALL DECcalculation
I_have_the_numberO: CALL BINcalculation
CALL DECcalculation
CALL HEXcalculation
I_have_the_numberB: CALL OCTcalculation
CALL DECcalculation
CALL HEXcalculation
I_have_the_numberD: CALL BINcalculation
CALL OCTcalculation
CALL HEXcalculation
exit: mov ax, 4c00h
int 21h
EnterBase PROC
MOV DX,OFFSET BASED_BUFFER ; GETS THE BASE
MOV AH,10
INT 21H
LEA DX,BASED_BUFFER[2]
MOV BL,BASED_BUFFER[1]
MOV BH,0
MOV BASED_BUFFER[BX+2],0
LEA SI, BASED_BUFFER[2]
XOR CX, CX
MOV CL, BASED_BUFFER[1]
LEA DI, TheBase
LOL_OF_BASE: MOV DL, [SI]
MOV [DI], DL
INC SI
INC DI
INC AL
RET
EnterBase ENDP
CheckBase proc
CMP TheBase,'H'
JE HEXBASE
CMP TheBase,'h'
JE HEXBASE
CMP TheBase,'O'
JE oCTALICbASE
CMP TheBase,'o'
JE oCTALICbASE
CMP TheBase,'B'
JE BINBASE
CMP TheBase,'b'
JE BINBASE
CMP TheBase,'D'
JE DECBASE
CMP TheBase,'d'
JE DECBASE
CMP TheBase, ' '
je ERRORoFBASE
ERRORoFBASE: MOV DX,OFFSET ERROR_STRING_BASE ;PUTS WORNG BASE Illegal_Number
MOV AH,9
INT 21H
JMP START
CheckBase ENDP
PRINTtheNUMBER PROC
MOV DX,OFFSET ENTER_STRING
MOV AH,9
INT 21h
MOV DX,OFFSET Number_buffer ; GETS THE number
MOV AH,10
INT 21H
LEA DX,Number_buffer[2]
MOV BL,Number_buffer[1]
MOV BH,0
MOV Number_buffer[BX+2],0
LEA SI, Number_buffer[2]
XOR CX, CX
MOV CL, Number_buffer[1]
LEA DI, The_numer
xor AL,AL
LOL_OF_NUMBER_CHECK: MOV DL, [SI]
MOV [DI], DL
INC SI
INC DI
INC AL
CMP AL,5
JE ERRORofNUMBER
LOOP LOL_OF_NUMBER_CHECK
RET
ERRORofNUMBER: MOV DX,OFFSET Illegal_Number ;PUTS WORNG BASE Illegal_Number
MOV AH,9
INT 21H
JMP START
PRINTtheNUMBER ENDP
PROC BINcalculation
XOR CX,CX
XOR AX,AX
MOV CX,4
MOV AX,16
LEA SI, The_binNumber[0]
TheBinarLoop: RCL The_numer,1
ADC [SI],0
INC SI
LOOP TheBinarLoop
ENDP
PROC OCTcalculation
ENDP
PROC DECcalculation
ENDP
PROC HEXcalculation
ENDP
code ends
end start
It should be look like this:
thanks!
שלו לוי
the algorighm to decode ascii strings from ANY base to integer is the same:
result = 0
for each digit in ascii-string
result *= base
result += value(digit)
for { bin, oct, dec } value(digit) is ascii(digit)-ascii('0')
hex is a bit more complicated, you have to check if the value is 'a'-'f', and convert this to 10-15
converting integer to ascii(base x) is similar, you have to divide the value by base until it's 0, and add ascii representation of the remainder at the left
e.g. 87/8= 10, remainder 7 --> "7"
10/8= 1, remainder 2 --> "27"
1/8= 0, remainder 1 --> "127"
Copy-paste next little program in EMU8086 and run it : it will capture a number as string from keyboard, then convert it to numeric in BX. To store the number in "The_numer", you have to do mov The_numer, bl :
.stack 100h
;------------------------------------------
.data
;------------------------------------------
msj1 db 'Enter a number: $'
msj2 db 13,10,'Number has been converted',13,10,13,10,'$'
string db 5 ;MAX NUMBER OF CHARACTERS ALLOWED (4).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 5 dup (?) ;CHARACTERS ENTERED BY USER.
;------------------------------------------
.code
;INITIALIZE DATA SEGMENT.
mov ax, #data
mov ds, ax
;------------------------------------------
;DISPLAY MESSAGE.
mov ah, 9
mov dx, offset msj1
int 21h
;------------------------------------------
;CAPTURE CHARACTERS (THE NUMBER).
mov ah, 0Ah
mov dx, offset string
int 21h
;------------------------------------------
call string2number
;------------------------------------------
;DISPLAY MESSAGE.
mov ah, 9
mov dx, offset msj2
int 21h
;------------------------------------------
;STOP UNTIL USER PRESS ANY KEY.
mov ah,7
int 21h
;------------------------------------------
;FINISH THE PROGRAM PROPERLY.
mov ax, 4c00h
int 21h
;------------------------------------------
;CONVERT STRING TO NUMBER IN BX.
proc string2number
;MAKE SI TO POINT TO THE LEAST SIGNIFICANT DIGIT.
mov si, offset string + 1 ;<================================ YOU CHANGE THIS VARIABLE.
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
The proc you need is string2number. Pay attention inside the proc : it uses a variable named "string", you have to change it by the name of your own variable. After the call the result is in BX: if the number is less than 256, you can use the number in BL.
By the way, the string is ALWAYS converted to a DECIMAL number.

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

Getting string input and displaying input with DOS interrupts MASM

In MASM, I created a buffer variable to hold the user string input from keyboard. I am stuck on how to hold the string input into that buffer variable. I don't have any libraries linked like the irvine ones and want to do this with DOS interrupts. So far I have something along the lines of
.model small
.stack 100h
.data
buff db 25 dup(0), 10, 13
lbuff EQU ($ - buff) ; bytes in a string
.code
main:
mov ax, #data
mov ds, ax
mov ah, 0Ah ; doesn't work
mov buff, ah ; doesn't seem right
int 21h
mov ax, 4000h ; display to screen
mov bx, 1
mov cx, lbuff
mov dx, OFFSET buff
int 21h
mov ah, 4ch
int 21h
end main
I assume using 0Ah is correct as it is for reading array of input of buffered characters.
I made some changes to your code. First, the "buff" variable needs the three level format (max number of characters allowed, another byte for the number of characteres entered, and the buffer itself) because that's what service 0AH requires. To use service 0AH I added "offset buff" (as Wolfgang said). Here it is:
.model small
.stack 100h
.data
buff db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 26 dup(0) ;CHARACTERS ENTERED BY USER.
.code
main:
mov ax, #data
mov ds, ax
;CAPTURE STRING FROM KEYBOARD.
mov ah, 0Ah ;SERVICE TO CAPTURE STRING FROM KEYBOARD.
mov dx, offset buff
int 21h
;CHANGE CHR(13) BY '$'.
mov si, offset buff + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, '$'
mov [ si ], al ;REPLACE CHR(13) BY '$'.
;DISPLAY STRING.
mov ah, 9 ;SERVICE TO DISPLAY STRING.
mov dx, offset buff + 2 ;MUST END WITH '$'.
int 21h
mov ah, 4ch
int 21h
end main
When 0AH captures the string from keyboard, it ends with ENTER (character 13), that's why, if you want to capture 25 characters, you must specify 26.
To know how many characters the user entered (length), access the second byte (offset buff + 1). The ENTER is not included, so, if user types 8 characters and ENTER, this second byte will contain the number 8, not 9.
The entered characters start at offset buff + 2, and they end when character 13 appears. We use this to add the length to buff+2 + 1 to replace chr(13) by '$'. Now we can display the string.
This is my code,maybe can help you.
;Input String Copy output
dataarea segment
BUFFER db 81
db ?
STRING DB 81 DUP(?)
STR1 DB 10,13,'$'
dataarea ends
extra segment
MESS1 DB 'After Copy',10,13,'$'
MESS2 DB 81 DUP(?)
extra ends
code segment
main proc far
assume cs:code,ds:dataarea,es:extra
start:
push ds
sub ax,ax
push ax
mov ax,dataarea
mov ds,ax
mov ax,extra
mov es,ax
lea dx,BUFFER
mov ah,0ah
int 21h
lea si,STRING
lea di,MESS2
mov ch,0
mov cl,BUFFER+1
cld
rep movsb
mov al,'$'
mov es:[di],al
lea dx,STR1 ;to next line
mov ah,09h
int 21h
push es
pop ds
lea dx,MESS1 ;output:after copy
mov ah,09h
int 21h
lea dx,MESS2
mov ah,09h
int 21h
ret
main endp
code ends
end start
And the result is:
c:\demo.exe
Hello World!
After Copy
Hello World!
You may follow this code :
; Problem : input array from user
.MODEL SMALL
.STACK
.DATA
ARR DB 10 DUB (?)
.CODE
MAIN PROC
MOV AX, #DATA
MOV DS, AX
XOR BX, BX
MOV CX, 5
FOR:
MOV AH, 1
INT 21H
MOV ARR[BX], AL
INC BX
LOOP FOR
XOR BX, BX
MOV CX, 5
PRINT:
MOV AX, ARR[BX] ;point to the current index
MOV AH, 2 ;output
MOV DL, AX
INT 21H
INC BX ;move pointer to the next element
LOOP PRINT ;loop until done
MAIN ENDP
;try this one, it takes a 10 character string input from user and displays it after in this manner, "Hello *10character string input"
.MODEL TINY
.CODE
.286
ORG 100h
START:
MOV DX, OFFSET BUFFER
MOV AH, 0ah
INT 21h
JMP PRINT
BUFFER DB 10,?, 10 dup(' ')
PRINT:
MOV AH, 02
MOV DL, 0ah
INT 21h
MOV AH, 9
MOV DX, OFFSET M1
INT 21h
XOR BX, BX
MOV BL, BUFFER[1]
MOV BUFFER [BX+2], '$'
MOV DX, OFFSET BUFFER +2
MOV AH, 9
INT 21h
M1: db 'Hello $'
END START
END

how to check compare if palindrome or not?

I don't know much of assembly language but I tried making this palindrome and it's quite hard. First I have to enter a string then show its original and reversed string then show if its a palindrome or not.
I already figured out how to show the reverse string by pushing and popping it through a loop from what I have read in another forum and figured it out
now the only problem for me is to compare the reverse string and original string to check if its a palindrome or not.
call clearscreen
mov dh, 0
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str1
int 21h
palin db 40 dup(?)
mov ah, 0ah
mov palin, 40
mov dx, offset palin
int 21h
mov dh, 1
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str2
int 21h
mov si, 2
forward:
mov al, palin + si
cmp al, 13
je palindrome
mov ah, 02h
mov dl, al
push ax 'push the letter to reverse
int 21h
inc si
jmp forward
palindrome:
mov dh, 2
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str3
int 21h
mov cx, 40 'pop each letter through a loop to show its reverse
reverse:
mov ah, 02h
pop ax
mov dl, al
int 21h
loop reverse
int 20h
clearscreen:
mov ax, 0600h
mov bh, 0Eh
mov cx, 0
mov dx, 8025
int 10h
ret
cursor:
mov ah, 02h
mov bh, 0
int 10h
ret
str1: db "Enter A String : $"
str2: db "Forward : $"
str3: db "Backward : $"
str4: db "Its a Palindrome! $"
str5: db "Not a Palindrome!$"
You have a string the user typed in, what you need to do is compare the first byte with the last byte, do the same for the 2nd one and the 2nd to last one. Keep doing this for the whole string. You also need the length of the string. To make life easier, you should convert the string to all UPPER case letters or all lowercase letters to make comparison easier.
It is unfortunate they are still teaching 16bit DOS code. This is a sample with the word defined in the data section. You will have to modify it to receive input and work on that string
.data
pal db "racecar"
pal_len equ $ - pal - 1
szYes db "yes$"
szNo db "no$"
.code
start:
mov ax,#data
mov ds,ax
call IsPalindrome
mov ah,4ch
int 21h
IsPalindrome:
lea si, pal
lea di, pal
add di, pal_len
mov cx, 0
CheckIt:
mov al, byte ptr [si]
mov dl, byte ptr [di]
cmp al, dl
jne No
inc si
dec di
inc cx
cmp cx, pal_len
jne CheckIt
mov ah,9
lea dx,szYes
int 21h
ret
No:
mov ah,9
lea dx,szNo
int 21h
ret
end start
For completeness and to bring us into the 21st century, 32bit NASM code:
section .data
fmt db "%s", 0
szPal db "RACECAR"
Pal_len equ $ - szPal - 1
szYes db "Yes", 10, 0
szNo db "No", 10, 0
extern printf, exit
global _start
section .text
_start:
call IsPalindrome
call exit
IsPalindrome:
mov ecx, 0
mov ebx, Pal_len
mov esi, szPal
.CheckIt:
mov al, byte [esi + ecx]
mov dl, byte [esi + ebx]
cmp al, dl
jne .No
inc ecx
dec ebx
jns .CheckIt
push szYes
push fmt
call printf
add esp, 4 * 2
mov eax, 1
jmp Done
.No:
push szNo
push fmt
call printf
add esp, 4 * 2
xor eax, eax
Done:
ret

Resources