Presentation is loading. Please wait.

Presentation is loading. Please wait.

Write a program to calculate x**y, given x and y.

Similar presentations


Presentation on theme: "Write a program to calculate x**y, given x and y."— Presentation transcript:

1 Write a program to calculate x**y, given x and y.
(revisited) inputs: x (w – because of the multiply instruction) y (b – it is a counter) output: ans (l – because of the multiply instruction) org $400 main move.w x,D0 get base move.w x,D2 and make copy move.b y,D1 get exponent beq skip if zero, skip calc loop mulu D0,D2 do product sub.b #1,D1 decrement counter bne loop skip move.l D2,ans save x**y stop #$2700 We have some problems: x dc.w 5 y dc.b 3 ans ds.l 1 end main

2 Valid flags for signed arithmetic
e.g. A – B if N=0 A – B 0 if N=1 A – B 0 if N=V A B if N≠V A B if N=V and Z=0 A B if N≠V or Z=1 A B V = 1 → result incorrect → signed overflow 0 → result correct Z = 1 → result is zero 0 → result is not zero N = 1 → result is negative 0 → result is positive (includes zero)

3 Decision Structures … if-then
in assembly language in pseudo-code: if condition then … code A end if … code B “if condition then” decomposes into a series of operations 1) test condition → sets flags 2) branch on the flags

4 Decision Structures … testing conditions
to “test” condition: arithmetic logical shifts, rotates data movement compare, test CMP <ea>,Dn [destination] - [source] e.g. move.b #5,D0 cmp.b #6,D0 TST <ea> compare [destination] to 0 ≡ [destination] – 0 e.g. move.b #-1,D0 tst.b D0

5 Conditional Branch …Bcc
M68000 Assembly Language [p16, N. Znotinas] conditional branches, an alternate view: BHI | BLS Br high |Br low or same BCC | BCS Br C clear |Br C set BHS*| BLO* Br high/same |Br low BNE | BEQ Br not equal |Br equal BVC | BVS Br V clear |Br V set BPL | BMI Br plus |Br minus BGE | BLT Br ≥ |Br < BGT | BLE Br > |Br ≤ * depends on assembler if synonym accepted

6 Decision Structures … if-then-else
in assembly language in pseudo-code: … if condition then … code A else … code B end if … code C Assembly code logic should flow from top to bottom like a waterfall.

7 Testing for out-of-range data/results
perform arithmetic input operation data result/data in range? → error continue processing input data and calculated data may be out-of-range, i.e. too large to store correctly you are responsible for testing for out-of-range conditions you would not test …

8 Testing for out-of-range results
e.g. ** technique 1 ADD.W D0,D1 B Ans_OK … code to put … out err msg JMP END_M Ans_OK … next code … section END_M e.g. ** technique 2 ADD.W D0,D1 B V_err … next code … section JMP END_M V_err … code to put … out err msg … END_M

9 Loops … conditional loops
in assembly language in pseudo-code: … while condition do … code A end while … code B

10 e.g. If a 16 bit number is negative, generate the absolute value of the number. Store the result in the number’s original memory location.

11 e.g. Find the larger of two 32 bit numbers.

12 M68000 Assembly Language [pdf, 92p; N. Znotinas]
Reading: M68000 Assembly Language [pdf, 92p; N. Znotinas] Look at the Bcc command and relate the use of the condition codes in the table to the relationships we developed in the last two lectures. The relationships are the same but written in different formats. Look at the unconditional branch commands: BRA, JMP review the operation of the various binary add (ADD, ADDA, ADDI, ADDQ) and subtract (SUB, SUBA, SUBI, SUBQ) instructions review the operation of the test (TST) instruction and the various forms of the compare (CMP, CMPA, CMPI) instruction review the operation of the MULS|MULU (multiply) and DIVS|DIVU (divide) instructions Expectations: starting with assignment 4, we will expect arithmetic results to be checked for out-of-range results when appropriate you can write any decision structure (if, if-then-else, if-elseif, case) in assembly language you can write a simple looping structure in assembly language you can select the correct conditional branch for any given signed or unsigned test


Download ppt "Write a program to calculate x**y, given x and y."

Similar presentations


Ads by Google