Related
I am a beginner in assembly and I am trying to make a program where I should input 2 strings from the keyboard. The first string should be the main string and the second input is the substring which I need to look for in the main string. If I find it, I should display that it was found, and if not, I should display that it wasn't found.
I tried to compare the lengths of the strings so that if the first one has less characters than the second, the message "Invalid" would be displayed. Then I tried to compare the substring with the string until the substring is found in the string and the message "string found" gets displayed, if not, the message : "string not found" gets displayed. No matter what words I input, it will always say "Invalid". How can I change that?
Here is my code:
.model small
.stack 200h
.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 ?
.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, 2
add di, 2
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
mov bp, cx ;CX is length string (long)
sub bp, bx ;BX is length word (short)
inc bp
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
check:
push si
push di
mov cx, bx ;BX is length of word
repe cmpsb
pop di
pop si
jne continue
jmp found_display
again:
mov si, ax
dec dx
lea di, InputWord
jmp matching
invalid_length:
mov ah, 09h
lea dx, invalid
int 21h
jmp done
found_display:
mov dx, offset found
mov ah, 09h
int 21h
jmp done
notfound_display:
mov dx, offset notfound
mov ah, 09h
int 21h
;fallthrough is intentional
done:
mov ax,4C00h
int 21h ;exit program and return to DOS
end start
I see that you have tried to apply some of the advice I gave in the answer at Finding the substring in an input string.
But it's gone wrong mostly because you decided to special-case where the inputted string has the same length as the inputted word. That's not a special case at all! If it so happens, my calculation of the number of possible finds will remain valid and yield a 1 in the BP register. In short, your problems originate from having inserted that valid part and not having edited the program accordingly.
add si, 2
add di, 2
je valid
jb matching
valid:
cld
repe cmpsb
je found_display
jne notfound_display
You don't need all of the above once you drop the redundant valid part.
again:
mov si, ax
dec dx
lea di, InputWord
jmp matching
And don't forget to remove any code that you don't actually need in your program, especially when you use code that you found on the internet.
Solution
...
; To check if the length of substring is shorter than the main string
mov cl, [si+1]
mov ch, 0
mov bl, [di+1]
mov bh, 0
mov bp, cx ; CX is length string (long)
sub bp, bx ; BX is length word (short)
jb notfound_display
inc bp ; -> BP is number of possible finds 1+
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
check:
push si
push di
mov cx, bx ; BX is length of word
repe cmpsb
pop di
pop si
jne continue
jmp found_display
...
Some optimization
I have absorbed the instructions cmp bx, cx ja invalid_length in the calculation of the number of possible finds (shaving off 2 bytes). If the subtraction produces a borrow, you know the word is longer than the string and so you can branch away. Whether you jump to invalid_length or notfound_display is up to you...
You can shorten this program by 2 bytes if you replace lea si, [InputString + 2] lea di, [InputWord + 2] by add si, 2 add di, 2.
This should work:
.model small
.stack 100h
print macro p
lea dx,p
mov ah,09h
int 21h
endm
.data
cn db 0
pn db 0
space db 10,13, " $"
msg db 10,13, "hjut$"
msg1 db "Introduceti primul sir:$"
msg2 db "Introduceti al doilea sir:$"
msg3 db "Al doilea sir nu se gaseste in primul.$"
msg4 db "Al doilea sir se gaseste in primul. $"
ar db 20 dup("$")
br db 20 dup("$")
.code
start:
mov ax,#data
mov ds,ax
mov si,01h
mov di,00h
mov cn,00h
print msg1
read1:mov ah,01h
int 21h
mov ar[si],al
inc si
cmp al,0dh
jnz read1
mov si,00h
print msg2
read2:mov ah,01h
int 21h
mov br[si],al
inc si
cmp al,0dh
jnz read2
mov si,00h
mov di,00h
jmp lop1
lop1: mov di,00h
inc si
mov bh,ar[si]
cmp bh,0dh
jz disp
mov bh,br[di]
cmp ar[si],bh
jnz lop1
jz lop2
lop2:inc si
inc di
mov bh,br[di]
cmp bh,0dh
jz l1
mov bh,br[di]
cmp ar[si],bh
jz lop2
jmp lop1
l1:
add cn,01h
dec si
jmp lop1
disp:
cmp cn,00h
jz disp1
print msg4
add cn,30h
mov dl,cn
mov ah,02h
int 21h
jmp exit
disp1:print msg3
exit:mov ah,4ch
int 21h
end start
I need help in combining two programs I have and I can't seem to get it working for me. Don't get the desired output.
So here's my problem statement:
Combine Two separate strings in a third string and display it, Where the first String is as it is and the second string is reversed.
Example:
Input:
String 1: 'Hello'
String 2: '.dlroW '
Output:
'Hello World.'
end of Example.
Now there are two ways we can go about this.
First: Use string functions.(Preferred)
Now I am fairly new to learning Assembly Language so I would like to do it using string functions so I can learn something New.
Second: Without using string functions.
Another Approach is if someone can help combining two programs, One for the concatenation of the string and the other for reversal, Note that I have written the two individual programs and they run well without any hiccups, I just can't seem to do it together. How I am going about with this is before concatenating the string I am trying to reverse it, then proceeding with the addition of the second string. But I can't seem to get it working. I've tried to the best of my knowledge.
//Concatenation Code
.model tiny
.data
msg1 db 10,13,"Enter the string 1: $"
cat db 30 DUP('$')
msg2 db 10,13,"Enter the string 2: $"
msg3 db 10,13,"Concatenated string is: $"
.code
mov ax,#data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
lea si,cat
up: mov ah,01h
int 21h
mov [si],al
inc si
cmp al,0dh
jnz up
lea dx,msg2
mov ah,09h
int 21h
dec si
up1: mov ah,01h
int 21h
mov [si],al
inc si
cmp al,0dh
jnz up1
lea dx,msg3
mov ah,09h
int 21h
lea dx,cat
mov ah,09h
int 21h
mov ah,4ch
int 21h
end`
Here's Part 2
//Reversal Code
.model tiny
.data
msg1 db 10,13,"enter the string: $"
string db 40 DUP('$')
rev db 40 DUP('$')
msg2 db 10,13,"reverse string is: $"
.code
mov ax,#data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
mov ah,0ah
lea dx,string
int 21h
lea si,string
lea di,rev
mov cl,[si+1]
mov ch,00h
add di,cx
inc si
inc si
up: mov al,[si]
mov [di],al
inc si
dec di
loop up
inc di
mov ah,09h
lea dx,msg2
int 21h
mov ah,09h
lea dx,[di]
int 21h
mov ah,4ch
int 21h
end
And Here is the code I came Up with by combining those two.
//That's the code I tried Combining
.model tiny
.data
.model tiny
.data
msg1 db 10,13,"Enter string1: $"
cat db 30 DUP('$')
msg2 db 10,13,"Enter string2: $"
msg3 db 10,13,"Concatenated string is: $"
.code
mov ax, #data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
lea si,cat
up: mov ah,01h
int 21h
mov [si],al
inc si
cmp al,0dh
jnz up
lea dx, msg2
mov ah,09h
int 21h
dec si
up2:mov al,[si]
mov [di],al
inc si
dec di
loop up2
inc di
up1:mov ah,01h
int 21h
mov [si],al
inc si
cmp al,0dh
jnz up1
lea dx,msg3
mov ah,09h
int 21h
lea dx,cat
mov ah,09h
int 21h
mov ah,4ch
int 21h
end
My Output
As you can see clearly I have failed at doing either task correctly. So can someone tell me where I am going wrong? Or teach me how to do this using the string Functions?
The up2 loop that tries to do string reversal comes too soon!. You've placed it where the 2nd string (the one that needs reversal) isn't even inputted yet.
If you would have written comments in your program, then you would probably have noticed this yourself.
This up2 loop uses the LOOP instruction that depends on the CX register but your program does not assign any suitable value to CX.
And also your working reversal program is using 2 buffers. Why then do you expect the combo to work from a single buffer?
Define the cat buffer so it can hold both strings.
Define the str buffer so it can hold the second string.
lea dx, msg1
mov ah, 09h ; DOS.PrintString
int 21h
lea di, cat
up: ; Input f i r s t string
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
mov [di], al
inc di
cmp al, 13
jne up
dec di ; Throw out the 13
; This marks the start of the reversed string, VERY IMPORTANT
; So don't change DI while inputting the 2nd string
lea dx, msg2
mov ah, 09h ; DOS.PrintString
int 21h
lea si, str
mov dx, si
up1: ; Input s e c o n d string
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
mov [si], al
inc si
cmp al, 13
jne up1
dec si ; Throw out the 13
cmp si, dx
je done ; Second string was empty. CAN HAPPEN!
up2: ; Reversed copying of s e c o n d string
dec si
mov al, [si]
mov [di], al
inc di
cmp si, dx
ja up2
done:
mov ax, 0A0Dh ; Add a proper carriage return and linefeed to the result
mov [di], ax
mov al, '$' ; Terminate the result with a dollar sign
mov [di+2], al
lea dx, msg3
mov ah, 09h ; DOS.PrintString
int 21h
lea dx, cat
mov ah, 09h ; DOS.PrintString
int 21h
First: Use string functions.(Preferred)
Both in the up loop and in the up2 loop, do you find next pair of instructions:
mov [di], al
inc di
Provided
the direction flag DF is clear so that DI can increment
the ES segment register points to #data
you can replace these 2 instructions by a single STOSB instruction.
This is what needs to go on top of your program:
.code
mov ax, #data
mov ds, ax
mov es, ax
cld
If we allowed ourselves to write a silly sequence of multiple std (set direction flag) and cld (clear direction flag) instructions, we could also replace mov al, [si] with lodsb. Care must be taken to keep a valid SI pointer (*).
dec si ; (*)
up2: ; Reversed copying of s e c o n d string
std
lodsb ; Due to STD, SI will decrement
cld
stosb ; Due to CLD, DI will increment
cmp si, dx
jae up2 ; (*)
done:
mov ax, 0A0Dh ; Add a proper carriage return and linefeed to the result
stosw
mov al, '$' ; Terminate the result with a dollar sign
stosb
In code that sets the direction flag (using std) it is best to end with a cld instruction so the direction flag is in the state we most expect!
I am trying to make a program where the user have to enter an input, for example: Hello World and get an output: 'DLROw OLLEh'. Here is my program
org 100h
include emu8086.inc
.DATA
STR1 DB 0DH, 0AH, 'Input: $'
STR2 DB 0DH, 0AH, 'Output: $'
Nl DB 0Dh, 0Ah,'$'
.CODE
START:
MOV AX, #DATA
MOV DS, AX
DISP:
LEA DX,STR1
MOV AH,09H
INT 21H
MOV CL,00
MOV AH,01H
READ:
INT 21H
MOV BL, AL
PUSH BX
INC CX
CMP AL, 0DH
JZ DISPLAY
CMP AL, 'A' ; < then A
JB NotALetter
CMP AL, 'Z' ; > then Z
JA AGAIN ; repeat again
JMP CONTINUE1
AGAIN:
CMP AL, 'a' ; < then a
JB NotALetter
CMP AL, 'z' ; > then z
JA NotALetter
CONTINUE1:
JMP READ
DISPLAY:
LEA DX, STR2
MOV AH, 09h
INT 21H
LEA DX, NL
MOV AH, 09h
INT 21h
POP BX ; pop enter key
ANS:
MOV AH, 02h
POP BX ; pop the character
CMP BL, 'a' ; check if its in upper case
JB toLower ; if yes then jmp to toLower
SUB BL, 32 ; if not in upper case then convert to upper case
JMP CONTINUE2
toLower:
ADD BL, 32 ; convert to lower case
CMP BL, 20h
;SUB BL, 32
CONTINUE2:
MOV DL, BL
INT 21H
LOOP ANS
JMP EXIT ; if everything is fine jmp to exit
NotALetter:
printn
print "The input character is not a letter."
EXIT:
hlt
.EXIT
END START
I can enter any input but as soon as I enter any symbol, I am getting a message that this is a symbol, then program ends whereas I want to get the same output but still allow to enter a space character. I am really new in Assembly and moreover while I was trying to figure everything out I got even more lost.
If I comment out JB NotALetter and JA NotALetter, my space character becomes # probably because I am adding 20 to the ASCII hex number. Can someone please help to figure out this problem?
I can enter any input but as soon as I enter any symbol, I am getting
a message that this is a symbol, then program ends whereas I want to
get the same output but still allow to enter a space character.
As OP wants to capture the space without messing with symbol message. This can be achieved with the following:
In the READ label after you compare for enter key add this:
CMP AL, ' ' ; compare for space
JZ CONTINUE1
And in the ANS label after you pop bx add this:
CMP BL, ' ' ; if equal to space
JZ CONTINUE2 ; then print it by going to CONTINUE2 label
Just add an extra comparison to your toLower method as follows:
toLower:
CMP BL, 'A'
JL CONTINUE2
ADD BL, 32 ; convert to lower case
Complete code:
org 100h
include emu8086.inc
.DATA
STR1 DB 0DH, 0AH, 'Input: $'
STR2 DB 0DH, 0AH, 'Output: $'
Nl DB 0Dh, 0Ah,'$'
.CODE
START:
MOV AX, #DATA
MOV DS, AX
DISP:
LEA DX,STR1
MOV AH,09H
INT 21H
MOV CL,00
MOV AH,01H
READ:
INT 21H
MOV BL, AL
PUSH BX
INC CX
CMP AL, 0DH
JZ DISPLAY
CMP AL, 'A' ; < then A
JB CONTINUE1
CMP AL, 'Z' ; > then Z
JA AGAIN ; repeat again
JMP CONTINUE1
AGAIN:
CMP AL, 'a' ; < then a
JB CONTINUE1
CMP AL, 'z' ; > then z
JA CONTINUE1
CONTINUE1:
JMP READ
DISPLAY:
LEA DX, STR2
MOV AH, 09h
INT 21H
LEA DX, NL
MOV AH, 09h
INT 21h
POP BX ; pop enter key
ANS:
MOV AH, 02h
POP BX ; pop the character
CMP BL, 'a' ; check if its in upper case
JB toLower ; if yes then jmp to toLower
SUB BL, 32 ; if not in upper case then convert to upper case
JMP CONTINUE2
toLower:
CMP BL, 'A'
JL CONTINUE2
ADD BL, 32 ; convert to lower case
CONTINUE2:
MOV DL, BL
INT 21H
LOOP ANS
JMP EXIT ; if everything is fine jmp to exit
;NotALetter:
; printn
; print "The input character is not a letter."
EXIT:
hlt
.EXIT
END START
Input Hello World,
Output DLROw OLLEh
Also, you don't really need NotALetter method as you can notice, I just commented out.
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.
I have writen this little experiement bootstrap that has a getline and print_string "functions". The boot stuff is taken from MikeOS tutorial but the rest I have writen myself. I compile this with NASM and run it in QEMU.
So the actual question: I've declared this variable curInpLn on line 6. What ever the user types is saved on that variable and then after enter is hit it is displayed to the user with some additional messages. What I'd like to do is to clear the contents of curInpLn each time the getline function is called but for some reason I can't manage to do that. I'm quite the beginner with Assmebly at the moment.
You can compile the code to bin format and then create a floppy image of it with: "dd status=noxfer conv=notrunc if=FILENAME.bin of=FILENAME.flp" and run it in qemu with: "qemu -fda FILENAME.flp"
BITS 16
jmp start
welcomeSTR: db 'Welcome!',0
promptSTR: db 'Please prompt something: ',0
responseSTR: db 'You prompted: ',0
curInpLn: times 80 db 0 ;this is a variable to hold the input 'command'
curCharCnt: dw 0
curLnNum: dw 1
start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096
mov ax, 07C0h ; Set data segment to where we're loaded
mov ds, ax
call clear_screen
lea bx, [welcomeSTR] ; Put string position into SI
call print_string
call new_line
.waitCMD:
lea bx, [promptSTR]
call print_string
call getLine ; Call our string-printing routine
jmp .waitCMD
getLine:
cld
mov cx, 80 ;number of loops for loopne
mov di, 0 ;offset to bx
lea bx, [curInpLn] ;the address of our string
.gtlLoop:
mov ah, 00h ;This is an bios interrupt to
int 16h ;wait for a keypress and save it to al
cmp al, 08h ;see if backspace was pressed
je .gtlRemChar ;if so, jump
mov [bx+di], al ;effective address of our curInpLn string
inc di ;is saved in bx, di is an offset where we will
;insert our char in al
cmp al, 0Dh ;see if character typed is car-return (enter)
je .gtlDone ;if so, jump
mov ah, 0Eh ;bios interrupt to show the char in al
int 10h
.gtlCont:
loopne .gtlLoop ;loopne loops until cx is zero
jmp .gtlDone
.gtlRemChar:
;mov [bx][di-1], 0 ;this needs to be solved. NASM gives error on this.
dec di
jmp .gtlCont
.gtlDone:
call new_line
lea bx, [responseSTR]
call print_string
mov [curCharCnt], di ;save the amount of chars entered to a var
lea bx, [curInpLn]
call print_string
call new_line
ret
print_string: ; Routine: output string in SI to screen
mov si, bx
mov ah, 0Eh ; int 10h 'print char' function
.repeat:
lodsb ; Get character from string
cmp al, 0
je .done ; If char is zero, end of string
int 10h ; Otherwise, print it
jmp .repeat
.done:
ret
new_line:
mov ax, [curLnNum]
inc ax
mov [curLnNum], ax
mov ah, 02h
mov dl, 0
mov dh, [curLnNum]
int 10h
ret
clear_screen:
push ax
mov ax, 3
int 10h
pop ax
ret
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
I haven't written code in Assembly for 20 years (!), but it looks like you need to use the 'stosw' instruction (or 'stosb'). STOSB loads the value held in AL to the byte pointed to by ES:DI, whereas STOSSW loads the value held in AX to the word pointed to by ES:DI. The instruction automatically advances the pointer. As your variable curInpLn is 80 bytes long, you can clear it with 40 iterations of STOSW. Something like
xor ax, ax ; ax = 0
mov es, ds ; point es to our data segment
mov di, offset curInpLn ; point di at the variable
mov cx, 40 ; how many repetitions
rep stosw ; zap the variable
This method is probably the quickest method of clearing the variable as it doesn't require the CPU to retrieve any instructions from the pre-fetch queue. In fact, it allows the pre-fetch queue to fill up, thus allowing any following instructions to execute as quickly as possible.