I am miserable at assembly programming and ask for your help. What I do not understand is why do I lose the characters of a string from my buffer even though there is plenty of room in it?
My readBuffer size is 32 and writeBuffer size is 64, what my program does is add a space after a period and makes the first letter capital.
EXAMPLE:
Good.morning.people hehehe in the jungle the mighty jungle //this is the data
Good. Morning. People hehehe in the jungle the mighty jungle //this is the output
WHAT MY PROGRAM DOES:
Good.morning.people hehehe in the jungle the mighty jungle //this is the data
Good. Morning. People hehehe in e jungle the mighty jungle //this is the output
As you can see I end up with 'e' instead of 'the'
Here is the my code which does the editing:
Read:
MOV bx, DataHandle
CALL ReadBuffer
CMP ax, 0 ;ax = how many symbols did I read
JE closeWrite ;if 0 then close write file
;Editing the string
MOV cx, ax ;CX FOR LOOPING
MOV si, offset rBuff ;read buffer = si
MOV di, offset wBuff ;write buffer = di
Do_It:
MOV dl, [si]
CMP dl, '.' ;comparing to period
JNE Keep_Going ;if not period put it in di (write buffer)
MOV dh, [si+1] ;put the char after the period in dh
CMP dh, ' ' ;check if space is after it
JNE If_capital ;if not I check if it is a capital letter
MOV dh, [si+2] ;if there was a space I add the char after the space
CMP dh, 'a' ;checking if it is a capital letter
JB Keep_Going
CMP dh, 'z'
JA Keep_Going
SUB byte ptr [si+2], 32 ;turn it into a capital letter
Keep_Going:
MOV [di], dl ;put the char into writebuff
INC si
INC di
LOOP Do_It
JMP Finishin
If_capital:
CMP dh, 'a'
JB Add_Space
CMP dh, 'z'
JA Add_Space
SUB byte ptr [si+1], 32
Add_Space:
MOV [di], '.'
INC di
MOV [di], ' '
INC di
INC si
JMP Do_It
;Write the result
Finishin:
MOV cx, ax
CALL checkOnScreen ;print it to command line
MOV bx, WriteHandle
CALL writeBuffer ;print it
CMP ax, rBuffSize ;compare the size to 32
JE Read ;if it was 32 then read again
You write only 32 bytes of wBuff. This is after the first run: "Good. Morning. People hehehe in ". Since the line is longer, the rest is behind the boarder of 32 byte. Then you reset the pointers to rBuff and wBuff and read the next block: "e jungle the mighty jungle". That block is attached to the written line.
Consider that the block to be written has a variable length. You can calculate the length by subtracting the offset of wBuff from DI (sub di, OFFSET wBuff) after changing the JMP Do_It to LOOP Do_It.
I can't show a working example since I can't interpret relevant parts of your code (CMP ax, rBuffSize, JE Read ???).
Thanks for input guys, I managed to do it myself this time. The problem was
Finishin:
MOV cx, ax
CALL checkOnScreen
MOV bx, WriteHandle
CALL writeBuffer
CMP ax, rBuffSize
JE Read
New code:
Finishin:
MOV cx, strLength2 ;length of writebuffer
CALL checkOnScreen
MOV bx, WriteHandle
CALL writeBuffer
CMP ax, rBuffSize ;ax was messed up so I changed it to strLength1 or readBuffer
JE Read
Related
I've been working on the project. The main goal is to calculate how many words do not contain letters from 'a' to 'k'. Given file has [0;1000] lines. Every line contains 6 columns.
The first two columns contain string with [1; 20] characters. Characters could be letters, numbers, and whitespaces.
3-5 columns contain integers in the range [-100; 100]. 6th column contains real numbers in the range [-9.99; 9.99] with only two digits after the decimal point.
Each section I separated by a semicolon ';'.
helloA;lB;lC;lD;lE;lF
A11;bas morning;0;0;5;1.15
My problem is, that if there is a newLine at the end of the file, skip them, and sum up them in the final count.
TASK: calculate how many words (the word is one or more symbols without ' '(space)) in the first two columns do not contain letters 'B' or 'C'. And print that integer number.
I have tried to compare al with 0x20 and jump to the next section if it is smaller, but that didn't help me.
What I have done so far
; Main
.whileNotTheEndOfFile:
mov si, 2
call procSkaitytiEilute
; Check the first two columns
;mov al, ';'
mov di, line
.skipSpaces:
mov al, [di]
inc di
cmp al, ' '
je .skipSpaces
cmp al, ';'
je .q3
dec di
.checkWord:
mov bx, 1
.q1:
mov al, [di]
inc di
cmp al, ' '
je .q2
cmp al, ';'
je .q2
jmp .q8
.q8:
cmp al, 20h
jl .skipLine
jmp .q7
.q7:
cmp al, 'A'
jl .q5
jmp .q4
.q4:
cmp al, 'K'
jae .q5
mov bx, 0 ; One or more invalid chars in current word
jmp .q1
.q5:
cmp al, 'a'
jae .q6
jmp .q1
.q6:
cmp al, 'k'
jae .q1
mov bx, 0
jmp .q1
.q2:
add [lineCount], bx ; BX=[0,1] Counting the 'good' words
cmp al, ';'
jne .skipSpaces
.q3:
dec si ; Next column?
jnz .skipSpaces
.skipLine:
cmp [readLastLine], byte 0
je .whileNotTheEndOfFile
; Jeigu ne failo pabaiga, kartojame cikla
.skipLine:
sub [lineCount], 2
cmp [readLastLine], byte 0
je .whileNotTheEndOfFile
; Hexadecimal convertion to decimal
mov dx, lineCount
mov ax, [lineCount]
call procUInt16ToStr
call procPutStr
macNewLine
mov si, dx
.writeToFile:
lodsb
cmp al, 0
jne .writeToFile
sub si, dx
lea cx, [si-1]
mov bx, [writingDescriptor]
mov ah, 40h
int 21h
; Closing Files
.end:
mov bx, [writingDescriptor]
call procFClose
.rasymoKlaida:
mov bx, [readingDescriptor]
call procFClose
exit
%include 'yasmlib.asm'
; void procSkaitytiEilute()
; Read line to buffer ‘eilute’
procReadLine:
push ax
push bx
push cx
push si
mov bx, [readingDescriptor]
mov si, 0
.loop:
call procFGetChar
; End if the end of file or error
cmp ax, 0
je .endOfFile
jc .endOfFile
; Putting symbol to buffer
mov [line+si], cl
inc si
; Check if there is \n?
cmp cl, 0x0A
je .endOfLine
jmp .loop
.endOfFile:
mov [readLastLine], byte 1
.endOfLine:
mov [line+si], byte '$'
mov [lineLength], si
pop si
pop cx
pop bx
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .data
readingFile:
db 'input.dat', 00
readingDescriptor:
dw 0000
writingFile:
times 128 db 00
writingDescriptor:
dw 0000
readLastLine:
db 00
line:
db 64
times 66 db '$'
lineLength:
dw 0000
lineCount:
times 128 db 00
My file:
XYZ;PPP;1;1;1;1.00
A11;bas aaa;0;0;5;1.15
My output: 4
Needed output: 2 (because there are only 2 "good" words "XYZ" and "PPP"), my program counts 2 more words because of a new line at the end.
It would appear that in contrast with the original task description your file can contain one or more empty lines as well. Or even lines with spaces followed by the newline codes.
To deal with these empty lines (only containing the bytes 13 and 10), your idea to skip on codes below 32 is good, but it's in the wrong place.
Below is the quick fix. Please notice that the ASCII codes are to be treated as unsigned numbers. Don't use the signed jl instruction on them.
mov di, line
.skipSpaces:
mov al, [di]
inc di
cmp al, ' '
je .skipSpaces
cmp al, ';'
je .q3
dec di
cmp al, 32 ; NEW
jb .skipLine ; NEW
.checkWord:
mov bx, 1
.q1:
mov al, [di]
inc di
cmp al, ' '
je .q2
cmp al, ';'
je .q2
! cmp al, 'A'
! jb .q1
! cmp al, 'K'
! jbe .badChar ; [A,K] is bad
! cmp al, 'a'
! jb .q1
! cmp al, 'k'
! ja .q1
! .badChar: ; [a,k] is bad
! mov bx, 0 ; One or more invalid chars in current word
jmp .q1
.q2:
add [lineCount], bx ; BX=[0,1] Counting the 'good' words
cmp al, ';'
jne .skipSpaces
.q3:
dec si ; Next column?
jnz .skipSpaces
.skipLine:
cmp [readLastLine], byte 0
je .whileNotTheEndOfFile
The lines that I marked with an exclamation mark really should be:
or al, 32 ; Make lowercase
cmp al, 'a'
jb .q1
cmp al, 'k'
ja .q1
mov bx, 0 ; [A,K] and [a,k] are badChars
Why does your program contain next redundant lines?
; Jeigu ne failo pabaiga, kartojame cikla
.skipLine:
sub [lineCount], 2
cmp [readLastLine], byte 0
je .whileNotTheEndOfFile
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
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
; multi-segment executable file template.
data segment
string db "THis is LuxUR in Summer."
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov bx, offset string
mov al, 0 ; lower letters in word
mov dl,0 ; maximum letters
check:
mov cl, 41h ; from A-Z
mov ch, 5Ah
mov ah, [bx]
cmp ah, "."
je dot
cmp ah, " "
je empty
jne letters
letters:
cmp ah, cl
je uppercase
inc cl
cmp cl, ch
jne letters
mov cl, 61h ; a-z
mov ch, 7Ah
lowercase:
inc al
cmp dl,al
jl maksimum
inc bx
jmp check
maksimum:
mov dl, al
inc bx
jmp check
uppercase:
inc bx
jmp check
empty:
mov al, 0
inc bx
jmp check
dot:
My program count lowercases in a word in al. and then puts in dl. (maximum lowercases)
I have label which name is dot. there I have to put some instruction by which I can print my result:
Summer is the word with the most lower cases 5
I try few instructions to do that but it doesnt work.
If you're using Windows, the easiest way is to use DOS Interrupts. Specifically, try interrupt 09. This takes a string and outputs it to the standard output.
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.