tags:

views:

655

answers:

4

assume that i have a program written in assembly language which takes an input sentence from the user ( combination of digits and letters) and on the next line will display the number of small letters in the sentence. Also display the number of digits in the sentence.

my question is: how i can make the count instruction to count the numbers and the letters ?

+3  A: 

I assume you mean x86 assembly and the string is null terminated.

mov eax, STRING_VARIABLE
xor ebx, ebx
xor ecx, ecx
.loop:
  mov dl, [eax]
  cmp dl, 0
  jz .end

  cmp dl, '0'
  jb .notdigit
  cmp dl, '9'
  ja .notdigit
  inc ecx
  jmp .notlowercase
  .notdigit:
  cmp dl, 'a'
  jb .notlowercase
  cmp dl, 'z'
  ja .notlowercase
  inc ecx
  .notlowercase:

  inc eax
  jmp .loop
.end:
; ebx contains the lowercase letter count
; ecx contains the digit count
Mehrdad Afshari
A: 

And if it was a Pascal string with the string length as the first byte you would modify as follows;

mov eax, STRING_VARIABLE
xor ebx, ebx  ; A tiny bit quicker and shorter than mov ebx,0
xor ecx, ecx
mov dh,[eax]  ; dh is loop counter based on string length
inc eax       ; move onto the string data
.loop:
  cmp dh,0
  jz .end
  .
  .
  .
.notlowercase:
  dec dh
  jmp .loop
.end:
Shane MacLaughlin
A: 

I think that Mehrdad has the general idea of what is trying to be accomplished;

Just a few observations, though --

a jump to .notlowercase following the "inc ecx" would save a few cycles, probably an oversight --

the last inc ecx I think should be inc ebx

A slight twist to the upper/lower test, given that this is only alpha/numeric characters, after the .notdigit label, the lowercase test can be replace with

.notdigit:
    and   dl, 0x20
    jz   .notlowercase
    inc   ebx
.notlowercase:

just my 2 cents -- :)

Borzio
A: 

This will also give incorrect results for Unicode strings. You'd need to increment eax by two, given UTF-16. If you'd need to count characters with the high-byte set, you'd need to account for that, too.