1

I have this assembly assignment I need to write and the task we need to do is accept user input and loop through each character and tally the number of alphabetic, numeric, and miscellaneous characters.

I found the easiest way to do this is to do three separate loops, one for counting the numbers, one for uppercase letter, and one for lowercase letter, than find the miscellaneous count by subtracting the numeric and alphabetic counts from the input string length.

I defined my alphabetic and numeric count variables to 0 in the .data section like this:

acount: db 0                  ; alphabetic count variable
ncount: db 0                  ; numeric count variable

So that I could increment them. All of my loops are set up the same way so here is my numeric counter as an example:

init_numeric:
        ;; Initialize the input for scanning
        mov     ecx, [rlen]             ; initialize the input length
        mov     esi, input              ; point to the start of input

scan_numeric:
        ;; beginning of the character scan for numeric values
        mov     al, [esi]               ; get a character
        inc     esi                     ; update to the next character
        cmp     al, '0'                 ; check the lower bound
        jb      not_num                 ; jump if below '0'
        cmp     al, '9'                 ; check the upper bound
        ja      not_num                 ; jump if above '9'
        inc     [ncount]                ; add 1 to the numeric count

not_num:
        dec     ecx                     ; update the number of characters
        jnz     scan_numeric            ; loop to top if more characters

Once these loops are complete, I'm getting the miscellaneous count, which is defined in the .bss section as:

mcount: resb 4            ; reserve space for misc character count

And the calculation and operations to find in as such:

get_misc:
        ;; Subtract the alphabetic and numeric counts from the length for
        ;; miscellanious character count
        mov     eax, [rlen]             ; move the input string length
        sub     eax, [acount]           ; subtract the alpha count
        sub     eax, [ncount]           ; subtract the numeric count
        mov     [mcount], eax           ; move eax value to mcount reserve

The problem is that when I run it, I get the user input perfectly fine but I get operation size undefined errors for the inc instructions, but when I do define them with dword or word, I get segfaults.

Any help please??


EDIT:

Here is my section for the output prompts and values:

result_write:
        ;; Write the results to the terminal

        ;; Alphabetic Count
        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, init               ; initial response msg
        mov     edx, ilen               ; initial msg length
        int     080h                    ; kernel execution

        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, [acount]           ; alphabetic count
        mov     edx, 4                  ; length
        int     080h                    ; kernel execution

        mov     eax, SYSCALL_WRITE      ; write function
        mov     ebx, STDOUT             ; file descripter
        mov     ecx, alpha              ; alphabetic response end
        mov     edx, alen               ; response length
        int     080h                    ; kernel execution

That is for the alphabetical count, the other two for numeric and misc. are identical.

1 Answer 1

3

You had ncount pointing to a db instead of dw or dd, which is why you can't use inc dword ptr [ncount] or inc word ptr [ncount]. You can use inc byte ptr [ncount], though.

Alternatively, widen ncount to a dw and use word ptr, or to dd and use dword ptr.

3
  • would I need to change mcount: resb 4 as well? and that fixed the errors, but now there are no numeric values being written to the terminal on run.
    – m_callens
    Feb 12, 2016 at 2:09
  • 1
    No, that correctly reserves 4 bytes (a dword). You haven't showed part of the code that does the output, so we can't tell what's wrong. Maybe you forgot to convert to text. Provide a minimal reproducible example and learn to use a debugger.
    – Jester
    Feb 12, 2016 at 2:10
  • how would you convert to text? sorry I'm new to assembly. Just move to register and add '0', then move back?
    – m_callens
    Feb 12, 2016 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.