Day 7

Compare and Jump

	Using the CMP (compare) instruction along with the conditional jump instructions,
we get the if construct of high level languages.

The CMP Instruction

The CMP Instruction is used to compare two things. It is used such as cmp ax,6 ; will compare AX and 6 cmp ah,bl ; will compare AH and BL var1 dw 0 cmp ax,var1 ; will compare AX and the value in var1 Note that CMP doesn't modify any registers or memory.

The Conditional Jumps

Conditional jumps jump to a label if the current condition (set by CMP) is true. The conditional jumps include: JMP - Will jump no matter what, doesn't check conditions. JE - Will jump if compared things are equal. JNE - Will jump if comparison is not equal. JA - Will jump if the first thing is greater. JB - Will jump if the first thing is less. Here's some sample code: mov ax,6 cmp ax,bx ; compare AX with BX ja Above ; jump to label Above if AX > BX je Equal ; jump to label Equal if AX == BX jb Below ; jump to label Below if AX < BX Note that you can't use RET with a jump and there are no conditional CALLs, you have to jump to somewhere that'll do the call and then jump back or something like that.

Some Code

This will output the numbers from 9 to 0: ;;---CODE START---;; mov ah,10 again: sub ah,1 cmp ah,0 ; check if AH is 0 je endofit ; if AH is 0 goto endofit push ax add ah,30h ; turn a digit into it's ASCII by adding 30h mov dl,ah mov ah,2 int 21h ; output the digit pop ax ; get the number back jmp again ; do it again endofit: mov ah,4ch int 21h ;;---CODE END---;; There's a better way to do that, if you find it, you don't get hit with an imaginary trout. Hope you enjoyed that! :)

This Day In Review

Hope you're having fun, I know I am! Happy coding!, - GbaGuy
Intro - Day 8

Patater GBAGuy Mirror