I'm having trouble with figuring out how to determine if a value is a number or letter in MASM assembly language. This program should go through and array and display the first number found in an array and print it along with the index it was found at. I'm using the Irvine32.inc library which contains IsDigit
but for some reason it isn't working and I don't know why.
Here's the code:
TITLE Number Finder
INCLUDE Irvine32.inc
.data
AlphaNumeric SDWORD 'A', 'p', 'Q', 'M', 67d, -3d, 74d, 'G', 'W', 92d
Alphabetical DWORD 'A', 'B', 'C', 'D', 'E'
Numeric DWORD 0, 1, 2, 3, 4, 5, 6
index DWORD ?
valueFound BYTE "number found: ", 0
atIndex BYTE "at index: ", 0
noValueFound BYTE "no numeric found", 0
spacing BYTE ", ", 0
;DOESNT WORK CORRECTLY
;SKIPS the value 67
.code
main PROC
mov esi, OFFSET AlphaNumeric ;point to start of array
mov ecx, LENGTHOF AlphaNumeric ;set loop counter
mov index, 0
mov eax, 0 ; clear eax
L1: mov al, [esi]
call IsDigit ; ZF = 1 -> valid digit , ZF = 0 -> not a valid digit
;jmp if digit
jz NUMBER_FOUND
;jmp if char
jnz CHARACTER
;this probably never gets reached
inc index
add esi, TYPE AlphaNumeric
loop L1
;if loop finishes without finding a number
jmp NUMBER_NOT_FOUND
;next iteration of loop if val is a char
CHARACTER:
add esi, TYPE AlphaNumeric
add index, 1
loop L1
NUMBER_FOUND:
mov edx, OFFSET valueFound
call WriteString ; prints "number found"
mov eax, [esi]
call WriteInt ; prints the number found
mov edx, OFFSET spacing
call WriteString
mov edx, OFFSET atIndex
call WriteString ; prints "at index: "
mov eax, index
call WriteDec ; prints the index value
;jmp to NEXT to skip NUMBER_NOT_FOUND block
jmp NEXT
NUMBER_NOT_FOUND:
mov edx, OFFSET noValueFound
call WriteString
NEXT:
exit
main ENDP
END main
When I debug it, when it gets the the loop iteration where it processes the value 67d it load 43 into al which is its hex representation but since 43h lines up with the ASCII value 'C' is assuming that call IsDigit
processes this as a letter and not a number. It also skips all numbers and will print "Number found: +65, at index: 10" which shouldn't even happen. Is there an operation I can use to convert the hex value to the decimal value for the IsDigit
call to work correctly? So if someone could please explain a way to evaluate if a value in an array is either a number or letter, capital and lowercase, that would be very much appreciated.