HW Day 13, Due
Monday Day 14
Re-assigned, Day 19 A. Run and
trace Program A,
watching
flags and program flow.
In each of the parts, start with the original Program A.
a) Modify Program
A
so that it marks a number Z, zero or N, nonzero. Hand in or email
code, hand in listing.
b) Modify Program
A
so that it marks a number P, strictly positive or N, not
positive (<0). Hand in or email
code, hand in listing.
With this one, fill in a tracing form, once for a Positive number run
and
once for a Not positive,
noting especially the PC and the flags.
Need more templates.?
Link is now also on 225 main page, bottom.
Re-assigned, Day 18 B. I wrote
today's programs so that the numbers
were stored in an absolute address (the equivalent of a global variable
in C++).
Modify Program B so that the numbers are stored on the user stack
(the equivalent of a local variable). (Day 12)
Hand in or email
code, hand in listing.
C. a) A very simple PEP program, no ifs, no loops, you may
use "global" type "variables". Get a number from input, and
multiply it by 8; output that.
Hint: 8 is a
power of 2. Multiplying by 2 can be done how?
b) Modify your program of part a to do
integer division by 8. For example, 16/8 = 2, 19/8 = 2, 26/8 = 3
Hand in or
email
code, hand in listings.
Re-assigned Day 19 Postpone D.
Write a program that gets two
numbers from input. If the second is smaller than the first,
output an S. (If not, do nothing). At the end, output "Bye".
Check it on input pairs 22 36, 22 22, 22 15. You may use
"global" type "variables." Hand in or email
code, hand in listing.
HW 11 comments: In
Microsoft C++ and
in Oren's C++ on Macintosh, int is 32 bits (8 hex digits), =
long.
Short is 16 bits (4 hex digits) =PEP word size.
Program B: w
is Unsigned Int: 32 bits. w=65536D fits
comfortably.
But w=216 , so w*w = 232 = 1
00 00 00 00H = 0 in a 32 bit word.
Output of powers of 6? link
D. a) We now know C++ will do logical ops on any kind
of integers including Char (you may get funny symbols for the low
numbers,
originally developed for the IBM PC. Or nothing. Microsoft
doesn't
put anything in the lower numbers, but does use the higher ones. )
Some ASCII links:
http://www.csgnetwork.com/asciiextset.html
http://www.fact-index.com/e/ex/extended_ascii.html
An article about Ascii extensions, historically
http://www.jimprice.com/jim-asc.htm
Scroll down to "IBM PC Extended ASCII Display Characters" to see the
two
most common versions.
b) Mask to change lower case to upper case: & m,
where m is 1101 1111, char m, can be
assigned
by:
m=0xDF, or
m=223 (decimal for DF),
m =(0)101 1111 works too since the MSBit of a letter
is always 0. So m =0x5F, or m=95, or m='_' (check the Ascii
table)
c) C++ shift right: Appears to use ASR if signed type
(-6--> -3)
but LSR if unsigned (65530--> 32765)
textp. 229 #13: Code: 70 00 05
00 09
F1 00 03
00
Notes:
Recall: ASR shifts 1 bit right. Integer
division by 2. "Lost
bit" (remainder) goes to Carry. Retains sign (fills in on left
with
0 or 1)
ASL shifts 1 bit left. Multiplication by 2.
Carry flags a result out of range.
Multiplication by other numbers? Work toward
general integer multiplication. By 4? by 8 ( for
hw.)
By 3?? 5? 6?
Flow of control: so far,
Unconditional
branch
BR target. Replaces PC so next instruction is at
location
target.
Conditional branches: If flag(s)
= 0/1 then BR target. (p. 241-2, A5)
BRV
If V=1 then PC<--Operand ; branch if signed overflow
BRC
If C=1 then PC<--Operand ; branch if unsigned
overflow (
Carry)
BREQ If
Z=1,
branch if last op gave
ZERO
BRNE If Z =0... branch if last op gave Not Zero
BRLT If
N=1, branch if last op gave NEG,
<0
BRGE If N =0... branch if last op gave Not Neg >0
BRLE If
N=1 or Z=1
"
not positive, <0
BRGT If N=0 and Z=0
"
POSitive, >0
Patterns for IF THEN ELSE
IF (condition) then Sequence T ELSE Sequence F
Computation affecting Flag
BR If
; Branch if flag/condition TRUE
"Else" body
Sequence F
BR EndIFELS ;Unconditional branch past
"If"
If: "If" body:
Sequence T;
EndIFELS: post-condition instructions
This maintains the "If", but the else and the if bodies are
reversed. Warford uses another version: Branch if the
condition is FALSE.
SEE HANDOUT
Note that the numerical branches (using Z or N) all have corresponding
negation branches, so you can branch if the condition is false or if
it's true, no problem. But there is no "Branch if C = 0", no
"Branch if V = 0" so you don't have a choice for those.
Examples on handout. (Paste-able below)
Start here Monday:
Branches must happen
as soon as the flag is set. E.g. insert
Adda 0x0000, i ; between Add and BRV.
Does nothing, but messes up flags.
(But: Flags stay, if not affected by next op.
Note Store and Output instructions don't
affect
flags. Could put between ADD and BRV. But dangerous!)
Branches are used very often to change flow if one number is 0, or
<,
>, etc. than another. How?
X<Y = = X-Y < 0 Branch if
X<Y
= = Branch if X-Y < 0
LdA X, d
SubA Y, d
BRLT target ;
Branch
if A register (X) is less than number subtracted (Y)
You don't care about the subtraction?
Use the Compare instruction!
LdA X, d
CPA Y, d
does the subtraction and sets flags, doesn't store result in A!
BRLT target
= = = = = = = =
| To Sievers Home Page |
CS225-Fall04/Day12.htm
|
|