D. a) Run C++ Program D, below, introducing & | ^ ~ (Note
^ for exclusive or is at odds with the logic usage). And
<<, >> for shifts.. Change the types to int, to
unsigned short, to char, and observe the results. (You may change
the values too if you like.) Comment on any interesting or
unusual findings.
b) Write a C++ program that gets a character from the keyboard and
makes it uppercase, whether it was upper or lower case as input. (Then
outputs it.) Don't use an if; use a mask. (This depends on
characters
being stored in ASCII, so is not machine-independent.) The
assembly
language version is p.228 #9.
c) Does the C++ shift operation >> fill in with 1's
("Arithmetic Shift
Right") or with 0's ("Logical Shift Right") when you shift right ?
Write a C++ program that takes a short integer and and shifts it
Right one place. Test it on 8, 12, 9. Do it to -6.
Decide
on the basis of your answer to this which it does. What if you
type
it unsigned (-6 signed is 65530 unsigned, in 2-byte word)?
Symbols: Textbook p. 229 #13, 15 (assume each is a complete
program.)
E. Rewrite p.228#9 to use Symbols throughout, and
immediate
addressing where appropriate. (Notice that substituting Symbols
for addresses means
our reason for putting data first--so addresses wouldn't get changed as
we altered the program--no longer applies. It is still a tidy way
to arrange things.)
p. 231 #25 (write a short program using symbols, immediate
addressing
).
F. Op. System. How many bytes (in decimal) does the
operating system in ROM take? (Loader & Trap Handler &
Machine Vectors)
Notes:
p. 221-2
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 an unsigned result out of range.
V flags a signed result out of range.
Multiplication by other numbers? Work toward
general integer multiplication. By 4, by 8? (By 16 for
hw.)
By 3??
ROL, ROR rotate bits out one end (into C) and in the
other. Use? Want Most sig. byte to switch
places with Least? 8 Rotates.
(We DON'T have Logical shift right (fills in on left with 0).
Don't have (e.g.) Rotate left n bits.)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Keeping track of addresses is a royal pain. Next step toward
readability,
toward a higher level language:
SYMBOLS. Syntax: Letter followed by letters
or numbers, no spaces. 8 or fewer characters. Case
sensitive.
A symbol is an alphanumeric label for
-- an address inputnum:
.block 2 ;for data
storage address
main:
deci 0x0005 ;for target of a branch
Symbol followed by colon: precedes the instruction or memory block to
be
labeled. (pp. 209-11)
--a constant value (e.g. to use in
immediate
mode) in front of .Equate <constant
value>
(pp.223-4)
linefeed: .equate 0x0A ; like C++
constant
The symbol (without the colon) can be used wherever the
address/constant
value would be used.
- - - - - - - - - - - - - - - - - - - - - - - -
;Program to use Deci and Deco, Day 9
Using
Symbols & Immediate Addressing
BR main
; was 0x 0007 ; branch around data!
PC<--0007
; DATA up front
one:
.equate 0x0001 ; was .word 0x0001 (gone using
immediate);
1 for adding to 1's complement
inputnum: .block
2
; 2 bytes= a word space for the decimal input
negnum:
.block 2 ; a word space for
the negative of the input.
linefeed:
.equate
0x0A
; was .byte 0x0A, gone using
immediate
; new line
; Now INSTRUCTIONS
main: deci inputnum
,d ;was deci 0x0005 ,d Input a decimal
number
deco inputnum,
d ; was deco 0x0005, d ;output the same decimal
number
charo
linefeed, i ; was charo
0x0A, i ; new line
lda
inputnum,d ;was
loada 0x0005 ,d ; load into Accumulator
NotA
;1's complement
AddA one,
i ;was AddA
0x0001, i ; Add 1 for 2's complement
Sta
negnum,d ;was
Storea 0x0005,d ;Store negative
deco
negnum,d ; was
deco
0x0005,d
; output the negative
STOP
.END
- - - - - - - - - - - - - - - - - - - - - - - - -
How does the assembler do it? Two passes:
Pass one:
Assign addresses to all lines: Create a Symbol Table
(alphabetical)
(equates and address labels mixed, no distinction.
No distinction after assembly.)
Symbol
Value
Symbol Value
inputnum
0003
linefeed 000A
main
0007
negnum 0005
one
0001
Pass two: Substitute value for symbol whenever it occurs,
translate
into machine code.
Compare C++ compiler: (pp. 218-20) Also must make
a
symbol
table
Symbol
Value
Type
inputnum
0003
sShort
Value is the Address of the space reserved. Type tells how
much space, among other things.
negnum
0005 sShort
linefeed
actual value? sShortConstant?
Type compatibility, type checking; disallows operations
inappropriate for type. (Adding a number to a character?)
Pascal: "Strongly
typed", C++, not so strongly.
C++ source--> machine code
C++ source--> assembly language--> machine code
(Could be a machine-independent abstract
proto-assembly
language? Java...)
(C++ global variables get treated this way. Local variables
(almost all are local, in good practice) are more complicated.)
+ + + + + + + + + + ++ + + + + + + + + +
Operating system: pp. 180-1 (Description is of what would
be IF it were on a real chip)
"UP" is toward 0 in memory. Down is toward FFFF
Application memory is between 0000 and FBCE. Program and data
start at 0000. User stack has "top" at FBCF, and builds FBCE,
FBCD, etc.
Below that is all Operating system:
System stack
I/O buffer
Loader
Trap handler
Machine vectors (store addresses of above 4
components)
Loader: When you hit the Load button, the
address of the first instruction of the Loader subprogram goes into the
Program Counter, the address of the System Stack goes into the Stack
Pointer, and the Von Neumann cycle starts (fetch-increment
PC-execute). When the Loader gets to its STOP, the program is in
the application memory. When you hit the Execute button,
PC is set to 0000 (where your program starts ), the stack pointer
(SP) is set to FBCF, the "top" of the user stack. And the Von
Neumann cycle starts.
When you call a Trap instruction (DECI, DECO, STRO) the Trap Handler
procedure is called, which contains the code for these; then you return
to your main program. These are written in Pep Assembly
language. You can see the code by checking Trace Load in the CPU
and Loading any program. The whole operating system will appear
in the Assembler Listing window.
//Program D Day 11. Using C++ bitwise
operations
#include <iostream>
using namespace std;
int main()
{
short numA=1, numB=3, numC; // 0000 0001, 0000
0011
// Example of the bitwise-AND operator
&
numC = numA &
numB;
// numC is now 1
cout<<numC <<endl;
// bitwise OR (inclusive)
|
numC = numA |
numB;
// numC is now 3
cout<<numC <<endl;
// bitwise OR (exclusive)
^
numC = numA ^
numB;
// numC is now 2
cout<<numC <<endl;
// bitwise NOT (1's complement)
~
numC = ~numB;
// numC is now -4, 1111 1100
cout<<numC<<endl;
//hex is a Manipulator that turns on hex
output
for numbers.
cout<<hex<<numC<<"in
hex"<<endl;
cout<<numC<<endl;
cout<<dec<<numC<<endl; //have
to restore usual with dec.
// bitwise shifts .
<<, >> [how weird is that?!]
cout<<numB<<endl;
numC = numB << 1; // shift B left one
bit.
numB>>1 shifts right one. numB<<3 shifts left 3 bits.
cout<<numC<<endl;
return 0;
}
= = = = = = = = = = = = =
| To Sievers Home Page |
CS225-Fall06/Day11.htm
|
|