Question

I am new to assembly language and while I was writing a procedure that tests ASCII value in AL for being a digit in the range “0 – 9”, I think I made a few logical mistakes.

Also I have to return true/false by setting/clearing Z flag. I know how to set/clear carry flag but I have not set/clear zero flag before.

I have tried to look around for help but I'm totally lost with this thing. Here's what I've done so far.

Include Irvine32.inc

.data
Array1 BYTE 0,1,2,3,'4',11,7,8,9,10
buff BYTE 10 dup(?)

.code
main PROC
Call Clrscr
Mov esi, Offset Array1
Mov edi, Offset buff
Mov ecx, LengthOf Array1
L1:
Mov AL,[ESI]
Call ASCII_Test
JNC No
Mov [EDI],AL
INC EDI
No:
INC ESI
Loop L1
Call CRLF
Mov edx, Offset buff
Call WriteString
Call CRLF
Exit
Main ENDP

ASCII_Test PROC
CMP AL,'0'
JB No
CMP AL,'9'
JA No
STC
RET
No:
CLC
RET
ASCII_Test ENDP

END main

Please tell me what mistakes I have made so I can correct them. Thanks.

Was it helpful?

Solution

I see several mistakes:

  1. you declared arrays of DWORD elements, but EDI and ESI are only being increased by 1 byte on each iteration; if I were you, I'd use arrays of bytes.
  2. the input array elements are literal numbers, not characters, so none of them will match as is; try changing one of elements to '4' instead of 4 to see the difference.
  3. you expect WriteString to print an array of DWORD elements, which is not a character string and for which you never specified the length.

As for ZF, there're no dedicated instructions to set and clear it (as it is with CF and a couple of other flags like IF and DF). You should know however that cmp instruction you're using modifies flags, so when you then check the flags with jb and ja and the jump occurs you could be sure that ZF is not set (see table here). If on the other hand jump didn't occur, ZF could be in either state (after second comparison, if AL was equal to '9' ZF would be 1, otherwise 0), so you'll need to set it manually. You could achieve this in many ways, but just to give you a simple example, this would do just fine:

cmp al, al ; as AL equals itself, ZF would be set after this instruction is executed
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top