'Assembly - How To Set A Boolean Variable

Can someone tell me how to set a Boolean variable in Assembly TASM? I have been looking on the Internet and I can't find a proper explanation.

Thanks to anyone who helps.



Solution 1:[1]

In the x86-64 and i386 SysV ABIs for example, variables of C's _Bool / bool type must the low byte of the register they're in set to 0 or 1, not just any non-zero value, so you can safely AND them together and stuff like that. Using setcc based on a condition is usually an easy way to do that.

A similar convention makes sense when you roll your own ABI, except in cases when you only need to test a return value for zero or non-zero. Then @dwelch's suggestion applies: don't waste an instruction booleanizing a value if you can just test the result with something that doesn't care where the non-zero bit is. See https://stackoverflow.com/tags/x86/info.

Solution 2:[2]

@dwelch is right, boolean is a high level concept, but assembly let you define constants that you might call TRUE and FALSE, and you can use them in your code. As a programmer you can create your own concept of "boolean".

Next example program defines TRUE and FALSE as constants, search for digits in a string, if digits are found, returns TRUE, otherwise returns FALSE, and, depending on the "boolean" result, it displays one message or another (made with EMU8086), I use 255 and 0 because it's easy with NOT to turn true (255) into false (0) and vice versa :

.model small
.stack 100h
.data
  FALSE equ 0
  TRUE  equ 255
  pass  db  'simple pa55word',0
  msj1  db  'Your password is OK$'
  msj2  db  'Your password requires at least one digit$'
  
.code
  mov ax, @data
  mov ds, ax

;SEARCH FOR ANY DIGIT IN PASSWORD.
  mov  si, offset pass ;PARAMETER FOR SEARCH_DIGITS.
  call search_digits   ;RETURNS BX = TRUE OR FALSE.

  cmp  bx, TRUE        ;IF BX == TRUE...
  je   good            ;...JUMP TO "GOOD" (ELSE, CONTINUES).

;THE PASSWORD HAS NO DIGITS.
  mov  ah, 9
  mov  dx, offset msj2
  int  21h  
  jmp  finale

;THE PASSWORD CONTAINS AT LEAST ONE DIGIT.      
good:  
  mov  ah, 9
  mov  dx, offset msj1
  int  21h  
  
finale:  
  mov  ax, 4c00h
  int  21h

;------------------------------------------                      
;PROC TO SEARCH FOR ANY DIGIT IN STRING SI.                  
;RETURN : BX = TRUE  : DIGIT FOUND.
;         BX = FALSE : NO DIGIT FOUND.
;MODIFIED REGISTERS : AL, BX, SI.

search_digits proc    
  mov bx, FALSE      ;NO DIGITS FOUND YET.
repeat:
  mov al, [ si ]
  inc si
  cmp al, 0          ;IF CHAR IS ZERO...
  je  done           ;STRING END REACHED.
  cmp al, '0'        ;IF AL < '0'...
  jb  repeat         ;...REPEAT (IT'S NO DIGIT).
  cmp al, '9'        ;IF AL > '9'...
  ja  repeat         ;...REPEAT (IT'S NO DIGIT).
  mov bx, TRUE       ;DIGIT FOUND!!!
done:
  ret
search_digits endp      

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2