We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26
FLOW CONTROL
MARS Assembly Language
Objectives After completing this lab, you will: • Get familiar with MIPS Jump and Branch instructions • Learn about pseudo instructions in MIPS • Learn how to translate high-level flow control constructs (if-then-else, for loop, while loop) to MIPS code MIPS Jump and Branch Functions MIPS Jump and Branch Functions For unconditional jump, the instruction j label is used where label is the address of the target instruction as shown below: MIPS Jump and Branch Functions BASED ON THE CONDITION WHETHER 2 REGISTERS ARE EQUAL OR NOT: MIPS Jump and Branch Functions Comparing the content of a register with zero (0): MIPS Jump and Branch Functions MIPS also provides four set on less than instructions as follows: MIPS Jump and Branch Functions For example, assume that $s0 = 1 and $s1 = -1 = 0xffffffff, then the following two instructions produce different results as shown below: Pseudo Instructions For example, the MIPS processor does not have the following useful conditional branch comparison instructions: Pseudo Instructions For example, the instruction blt $s0, $s1, label can be implemented using the following sequence of two instructions: Pseudo Instructions Similarly, the instruction ble $s2, $s3, label can be implemented using the following sequence of two instructions: Pseudo Instructions More examples of pseudo instructions. Note that the assembler temporary register $at=$1 is reserved for its own use. Translating High-Level Flow Control Constructs We can translate any high-level flow construct into assembly language using the jump, branch and set-less-than instructions. For example, let us consider the following if statement: Translating High-Level Flow Control Constructs Let us assume that variables a, b, c, d, e are stored in registers $s0 thru $s4 respectively. The following assembly code implements this IF statement: Translating High-Level Flow Control Constructs We can also implement an IF statement with a compound condition involving logical AND operation. For example, let us consider implementing the following IF statement: Translating High-Level Flow Control Constructs The IF statement is implemented efficiently using the following assembly code which uses the fall through concept which skips the execution of the instruction if the first condition is false otherwise it continues the execution: Translating High-Level Flow Control Constructs Similarly, we can translate an IF statement with a compound condition involving logical OR operation. For example, let us consider implementing the following IF statement: Translating High-Level Flow Control Constructs The IF statement is implemented efficiently using the following assembly code which checks the first condition and if it is true, it skips the second condition: Translating High-Level Flow Control Constructs We can also implement all types of loops. Let us consider implementing the following for loop: Translating High-Level Flow Control Constructs Let us assume that variable i is stored in register $s0 and n is stored in register $s1. Then, the for loop is implemented using the following assembly code: Translating High-Level Flow Control Constructs Consider the implementing of the following while loop: Translating High-Level Flow Control Constructs Finally, let us consider implementing the following do-while loop: Translating High-Level Flow Control Constructs The do-while loop can be translated using the following assembly code: Laboratory Exercises 1. Write a program that asks the user to enter an integer and then displays the number of 1's in the binary representation of that integer. For example, if the user enters 9, then the program should display 2. 2. Write a program that asks the user to enter two integers: n1 and n2 and prints the sum of all numbers from n1 to n2. For example, if the user enters n1=3 and n2=7, then the program should display the sum as 25. 3. Write a program that asks the user to enter an integer and then display the hexadecimal representation of that integer. Get in Touch With Us Send us a message or visit us City of Batac, Ilocos Norte, Philippines (63) 77-600-0459 [email protected]