Programs

Programs consist of 4 basic structures

  1. Code Block

  2. if-then-else

  3. while

  4. do-until (for optimization)

Code block

  • block of instructions executed consecutively

  • No branches, jumps, or subroutine calls

If Then

************************************************

#define FINAL 10

int count;

if (count == FINAL) then {block}

else {else-block)

************************************************

In assembly…

FINAL        EQU        10                (constant embedded in the machine code itself)

                 ORG        $B000         (data section)

COUNT     RMB        1

                 ORG         $C000         (program section)

IF              LDAA        COUNT       (load COUNT into A)

                 CMPA       #FINAL        (Compare A register to const FINAL)

                 BNE           ELSE        (branch is the false path, Branch if not equal)

THEN      {then-block}

                 BRA           ENDIF        (jump over the else block, branch always)

ELSE       {else block}

ENDIF     {next instruction}

************************************************

While loop

#define FINAL 10

int count;

while( count ≠ FINAL) {block}

************************************************

FINAL        EQU        10                (constant embedded in the machine code itself)

                 ORG        $B000         (data section)

COUNT     RMB        1

                 ORG         $C000         (program section)

WHILE      LDAA       COUNT       (load COUNT into A)

                 CMPA       #FINAL 

                 BEQ          ENDWLE        (branch if equal)

                 {block}

                 BRA           WHILE

ENDWLE {next instruction"}

Do Until/While

#define FINAL 10

int count;

do {block} until (count == FINAL)

************************************************

FINAL        EQU        10                (constant embedded in the machine code itself)

                 ORG        $B000         (data section)

COUNT     RMB        1

                 ORG         $C000         (program section)

DO            {block}    

UNTIL       LDAA        COUNT

                 CMPA        #FINAL

                 BNE           DO

ENDDO. {next instruction"}

block will be executed at least once, whereas a while might not be executed at all

Ways to implement variables

  1. in memory

    1. Slower program because you first have to load it into the ALU

    2. Larger program because of the load instructions

  2. in register

    1. limited number of registers

For-Loop

#define N 10

int count;

for(count=0; count<=N; count++) {block}

Implementation should be data independent (i.e. value of N)

int count → signed numbers → loop should not be executed at all if N<0 → while structure

count = 0;

while(count<=N) {

    block;

    count++;}

************************************************

Count variable in memory

                 ORG            $B000

N             EQU             10

COUNT    RMB             1

                ORG             $C000

                CLR             COUNT

WHILE    LDAA            COUNT

               CMPA            #N

               BGT               ENDWHILE

               {block}

               INC                COUNT

               BRA               WHILE        

ENDWHILE {next instruction}

***********************************************

Count variable in register

N             EQU             10

                ORG             $C000

                CLRA

WHILE     CMPA            #N

                BGT               ENDWHILE

                {block}

                INCA               

                BRA               WHILE        

ENDWHILE {next instruction}               

       

Program Example:

Write an assembly language program that adds the first N+1 integer numbers (0<=N<255); SUM = 0+1+2+…+N

Pseudo code:
void main(){

    unsigned int SUM

FCB declares AND initializes a variable

EQU just declares