Assembly: Branch programming

Posted by Chalks on Mon, 16 Mar 2020 12:56:54 +0100

Branch programming

(1) Simple branch program: two branches are obtained by judging a condition, and the judgment result of the condition determines the execution direction of the program. The disadvantage lies in the limitation of execution flow direction, for example, when the condition is a multi segmented function, the requirement cannot be satisfied.

Does the Created with Rapha ë l 2.2.0 start block meet the conditions? End yesno

Example: try to program the program of Y=MAX (X1, X2, x3). The program flow chart is as shown in the figure (let X1, X2, X3 be stored in three consecutive units starting with BUF, and all of them are unsigned 8-bit binary numbers)

.486
DATA   SEGMENT
   BUF   DB   21,30,98
     Y      DB    ?
DATA    ENDS
CODE   SEGMENT
            ASSUME   CS:CODE,DS:DATA
START:    MOV    AX,DATA
                MOV   DS,AX
                LEA     SI,BUF
               MOV   AL,[SI]
               CMP   AL,[SI+1]
               JNB     NEXT                 ;If X1 Not less than X2,Then turn to NEXT
               MOV   AL,[SI+1]           ;If X1 less than X2,Will A2 Send AL               
NEXT:     CMP   AL,[SI+2]
               JNB    NEXT1
               MOV  AL,[SI+2]
NEXT1:   MOV   Y,AL
               MOV  AH,4CH
               INT     21H
CODE     ENDS
               END  START

(2) Multi branch program: in order to solve the shortcomings of single branch program, the jump TABLE method is used to realize the design of multi branch program. For example, if we want to design a 4-branch program, define a TBLE jump TABLE in the data section, which stores 4 CASE addresses respectively. Each address occupies two units (two bytes) of memory, so the address of CASE0 is stored in the place where the address in the data segment is TBLE, that is, the TABLE head. Assuming that when CASE0 is met, skip to the place where CS segment address is 122AH, then TABLE stores 2AH, and TABLE+1 stores 12H.

Jump table address Entrance label address Serial number
TBLE R0 low byte + R0 high byte 0
TBLE+2 R1 low byte + R1 high byte 1
TBLE+4 R2 low byte + R2 high byte 2
TBLE+8 R3 low byte + R3 high byte 4

Example: try programming: INPUT the numerical sequence number 0 ~ 4 after the prompt string "INPUT", and turn to one of the five branch program segments according to the number INPUT by the keyboard. Assuming that the entry address label of each branch program segment is R0-R4, if the number n is entered, the Rn program segment will be confirmed and "OUTPUT:n" will be displayed. When the Enter key is pressed, the program ends. It is required to use the jump table to realize the transfer (each branch program segment and the main program are in the same code segment).

.486
DATA  SEGMENT
  TBLE   DW    R0,R1,R2,R3,R4   ;Automatic will Rx Fill in the offset address of
MESS1  DB    'INPUT:$'  ;
MESS2  DB    'OUTPUT:$'
DATA   ENDS
STACKA  SEGMENT PARA STACK'STACK'  ;PARA Define segment head address(20 The last 4 bits of 0)Is an integral multiple of 16
               DW  50 DUP(?)
STACKA  ENDS
CODE   SEGMENT
             ASSUME   CS:CODE,DS:DATA,SS:STACKA
MAIN   PROC  FAR  ;Subprogram definition instruction PROC,FAR Call between segments
START:  PUSH  DS
              MOV   AX,0
              PUSH  AX      ;Stack the contents of the registers that will be used in the subprogram
              MOV    AX,DATA
              MOV    DS,AX
              MOV    AX,STACKA
              MOV    SS,AX        ;16~19 Segment register description statement
AGAN:   LEA      DX,MESS1
              MOV    AH,09H
              INT       21H            ;   DOS09 Function call
              MOV    AH,01H
              INT       21H           ;DOS01 Function call
           CMP        AL,0DH;     Do you want to type carriage return ascii The code is 0. DH
              JZ         EXIT 
              CMP     AL,'4'
              JA         AGAN
              AND     AL,0FH   ;take ascii Code to number, 0~9 Corresponding hex ascii30~39
              MOVZX  AX,AL
              SHL        AX,1
              MOV     SI,AX
              LEA       DX,MESS2
              MOV    AH,9
              INT      21H
              JMP     TBLE[SI]      ;Turn to sequence number block,Register relative addressing 
R0:         MOV   DL,'0'
              JMP      DISP
R1:         MOV    DL,'1'
              JMP      DISP
R2:         MOV   DL,'2'
              JMP      DISP
R3:        MOV    DL,'3'
              JMP      DISP
R4:        MOV    DL,'4'
              JMP      DISP
DISP:      MOV   AH,02H
               INT     21H         ;
               JMP    AGAN
EXIT:       RET
MAIN      ENDP
CODE      ENDS
               END START

Note: in the 01H,02H,09H functions of DOS, the input and output in the program are ASCII codes corresponding to characters, rather than direct numbers.

Published 1 original article, praised 0 and visited 5
Private letter follow

Topics: ascii Programming less