Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

finding smallest letter in a given array of characters

r0nnr0nn Member Posts: 10
Hi,

I need to implement a function that finds the smallest letter in a given array of characters (according to its ASCII code), capitalizes it if necessary and then returns it. The function has the following input: an array of characters and the size of the array. Storing the result in dl.

I have this code but I can't get it to work properly

[code]
mov dl, 0x7f // initialize DL
mov dh, 0x61 // first value of small letter
mov ah, 0x20 // difference of small letter to capital letter
mov ecx, 0 // counter
mov ebx, arrayOfLetters // initialize array to ebx


FOR_EXPR:
cmp ecx, arraySize // loop from 0 to arraySize
jge END_FOR
mov al, byte ptr[ebx + ecx * 1] // reads the values of the array
cmp al, dl // compare max value to current item of the array
jl MIN_FOUND
inc ecx
jmp FOR_EXPR

END_FOR:
cmp dl, dh
jge CAPITALIZE

MIN_FOUND:
mov dl, al


CAPITALIZE:
sub dl, ah // capitalizes the letter
[/code]

The characters in the array is "cOmPuTeR"
When I try to run this code, I get the output "C" which is incorrect.(It should be "O") I figured that my code only goes through the first pass and doesn't loop so in my MIN_FOUND I added
[code]
MIN_FOUND:
mov dl, al
jmp FOR_EXPR
[/code]
but when I try to compile it, it won't run and gives me an unhandled exception.

I appreciate any input or suggestions thanks.

Comments

  • BretBret Member Posts: 114
    A couple of things (could do many more, but we'll just fix it so it works). First of all, for your application, you should be treating these as unsigned numbers, not signed (you should be using JA/JB rather than JG/JL jumps). Your code should work either way since the numbers are small, but it's just generally good practice to treat numbers as unsigned by default unless you are in a special section where you are actually doing math on signed numbers.

    Your attempted patch was a step in the right direction: you need to store the "new" smallest letter each time you find it. However, in your attempted patch, you jump out of the loop before you increment the loop counter (ECX), and then jump back to the beginning of the loop. As a result, ECX never changes and you keep reading the same character over and over and over again (an infinite loop). You need to keep the MIN_FOUND section inside the loop, e.g:

    [code]
    cmp al,dl
    jae NOT_MIN
    mov dl,al
    NOT_MIN:
    inc ecx
    [/code]
  • r0nnr0nn Member Posts: 10
    Thanks for responding. My code now looks like this, but its still not giving me the correct output. What I'm getting now is "c"

    [code]
    FOR_EXPR:
    cmp ecx, arraySize
    jge END_FOR
    mov al, byte ptr[ebx + ecx * 1]
    cmp al, dl
    jge NOT_MIN
    mov dl, al
    jmp FOR_EXPR

    END_FOR:
    cmp dl, dh
    jge CAPITALIZE

    CAPITALIZE:
    sub dl, ah


    NOT_MIN:
    inc ecx
    [/code]
  • r0nnr0nn Member Posts: 10
    Never mind, I solved the problem. Thanks again for your help.
  • darkmafiaguydarkmafiaguy Member Posts: 2
    How did you solve the problem? Would you mind posting your final code or how to fix the problem for clarity? Thanks.
  • r0nnr0nn Member Posts: 10
    Same as the code above, the only thing I'm missing is to increase the counter before looping back.
  • darkmafiaguydarkmafiaguy Member Posts: 2
    I did that. I still receive the first letter of the array only, however.
  • r0nnr0nn Member Posts: 10
    Here's my exact code
    [code]
    FOR_EXPR:
    cmp ecx, arraySize
    jge END_FOR
    mov al, byte ptr[ebx + ecx * 1]
    cmp al, dl
    jge NOT_MIN
    mov dl, al
    inc ecx
    jmp FOR_EXPR

    END_FOR:
    cmp dl, dh
    jge CAPITALIZE

    CAPITALIZE:
    sub dl, ah


    NOT_MIN:
    inc ecx
    [/code]
Sign In or Register to comment.