|
cmp srcReg, op Computes the difference srcReg − op and sets the status flags (N,Z,V,C) according to the outcome of the subtraction You can use as op: 1. a register 2. a small constant (between -1000 and 1000) The effect of a subtraction is to compare the value in register srcReg against the value specified by op The register srcReg is NOT updated by the compare instruction ! Only the N,Z,V,C flags are updated |
// File: /home/cs255001/demo/asm/6-if/cmp.s main: mov r0, #10 // r0 = 10 // Compare 2 equal values: check the flags !!! cmp r0, #10 // N=0, Z=1, C=1, V=0 // Compare against a smaller value: check the flags !!! cmp r0, #6 // N=0,Z=0,C=1,V=0 // Compare against a larger value: check the flags !!! cmp r0, #16 // N=1,Z=0,C=0,V=0 |
Look carefully at the flags when you run the program.
|
|
BUT only 6 of them are relevant to compare signed values:
We will only study (and use) these 6 conditional branch instructions:
beq - branch on equal condition bne - branch on not equal condition bgt - branch on greater than condition blt - branch on less than condition bge - branch on greater than or equal condition ble - branch on less than or equal condition |
All the conditional branch instructions operate the same way.
I will discuss one of them in details first, and then the other 5 will be described just briefly.
blt label If the status flags, at the moment that this instruction is executed, indicates that the cmp operation resulted in a "LESS THAN" condition, the CPU will "branch" or "jump" to the instruction marked by label If the status flags indicates the otherwise (i.e., "NOT LESS THAN"), then the CPU will continue the execution with the next instruction (i.e., the instruction that follows the blt instruction) |
mov r0, #5 // r0 = 5 - try changing this to 15
// Compare 2 values...
cmp r0, #10
blt there // Branch to label "there" if r0 < 10
mov r1, #4444
mov r2, #4444
mov r3, #4444
there:
mov r4, #4444
mov r5, #4444
mov r6, #4444
|
Effect of the "blt there" instruction in this program is as follows:
|
How to run the program:
|
Try changing the instruction mov r0, #5 to mov r0, #15 and run the program again - it will not branch in this case !!!
|
|
|
|
We will examine each control statement and give the "blue print" on how the control statement can be translated into assembler code.