.model small
.stack 100H
.data
A db ' this is a test $'
.code
mov ax, @data
mov ds, ax
mov si, 0
mov cx, 0
myloop:
cmp A[si], '$'
je final
cmp A[si], ' '
inc si
je count
jmp myloop
count:
inc cx
jmp myloop
final:
mov dx, cx
mov ah, 9
int 21h
end
Asked
Active
Viewed 1,208 times
-2

Weather Vane
- 33,872
- 7
- 36
- 56

Chen Kahalany
- 3
- 3
-
2Click "edit" below your question, select all your code, click the button with the curly braces, save your question. – Jose Manuel Abarca Rodríguez Jun 06 '16 at 21:09
-
2Also, comment your code and learn to use a debugger. Furthermore `int21/09` expects a pointer to a string, it will not print a number. – Jester Jun 06 '16 at 21:11
-
1You need to convert CX into string = http://stackoverflow.com/questions/37605815/how-can-i-print-0-to-100-in-assembly-language-in-emu-8086/37618809#37618809 – Jose Manuel Abarca Rodríguez Jun 06 '16 at 21:13
-
3Note that `inc si` affects the `Z` flag, and destroys the result of the comparison `cmp A[si], ' '`. So the following `je count` is not testing what you think it does. – Weather Vane Jun 06 '16 at 21:59
-
2...and since `si` started at `0` incrementing it will cause "not zero" so you won't ever count any spaces. – Weather Vane Jun 06 '16 at 22:19
1 Answers
0
you override the flags for the "is blank" comparison by the subsequent "inc si"
.model small
.stack 100H
.data
A db ' this is a test $'
.code
mov ax, @data
mov ds, ax
mov si, 0
mov cx, 0
myloop:
cmp A[si], '$'
je final
cmp A[si], ' '
jne do_not_count ; skip count if it's not a blank
count:
inc cx ; it is a blank, count it
do_not_count:
inc si
jmp myloop
final:
;mov dx, cx ; this does NOT print CX
;mov ah, 9
;int 21h
mov dl, cl ; workaround: this works for cx < 10
add dl, '0'
mov ah, 2
int 21h
end

Tommylee2k
- 2,683
- 1
- 9
- 22
-
-
you're right. to do this, in "final" CX must be converted to a decimal string, and this has to be printed. – Tommylee2k Jun 08 '16 at 06:55
-
the OP didn't really state what exactly he wanted. If his problem is "CX stays 0", counting the spaces is the solution, not printing CX ;) – Tommylee2k Jun 08 '16 at 07:04