Description
For this assignment, you are to write the max function in 8051 assembly and test it in EdSim51.
The pseudocode is shown on the left, and the template for the assembly is on the right:
// C-like version pseudocode
char max(char a, char b) { if (a > b) {
void main() { char t = 0; if (max(-3, -2) != -2) { goto Error; } else { t++; } …. // more test cases Success: goto Success; Error: goto Error;
|
;; template for hw1.asm
;; Put status of your code here: does it work? has bugs? etc ORG 0H max: ;; parameters a, b are in R0, R1; ;; return value should be in accumulator A ;; compare R0 and R1 (by subtraction, SUBB) ;; if R0 larger (check carry/borrow bit) ;; jump (JC? JNC?) to copy R0’s value into A, ;; return (RET) ;; else copy R1’s value into A, ;; return ;; declare your own labels as needed. main: ;; use R2 as the variable t, initialize to 0 ;; put test parameters into R0 and R1 ;; call the max subroutine ACALL max ;; compare return value (A) with answer ;; if not the same (JZ? JNZ?), jump to Error ;; otherwise, increment R2, continue testing …. Success: SJMP Success Error: SJMP Error END
|
For the max subroutine, you are to use R0 and R1 for the parameters to compare, and return the larger number in the accumulator A. The main program keeps calling the max function several times with different test cases. Instead of you having to manually step through each case, your code should compare the return value with your expected value and keep track of which test case you are on (using the local variable t, to be implemented with the register R2). On the first failed case, jump to the label Error; else keep testing until you hit the Success infinite loop.
Why loop forever like this? because when you simulate it, it goes by very fast and you don’t want to have to step through each case each time. So, just have the simulator run. You can observe the program counter (PC), which will stay at one of the two loops. You can take a look at the register R2 in the simulator’s window. If you have three test cases and all three pass, then R2 should show 3. Otherwise, R2 has the value of the first test case that failed.
For call and return, use the ACALL instruction to call a function with the target label, and RET instruction to return.
If you have to jump, you can use SJMP for short jump.
For comparison, you can subtract using the SUBB instruction. If a borrow is required, then the C (carry flag) is set. Then, you can use the JC or JNC instruction (jump if carry bit / not carry bit) to do the right thing.
You can take a look at the instruction set manual (on Keil’s website) for reference.
When you are done, please turn in the file named hw1.asm at the EEE Drop Box. The EEE Drop Box automatically takes care of renaming it, so please don’t encode your user name or student ID in the file.