Transcript R2 0
TCSS 490A
Topics in Computing & Software Systems
“Introduction to Computer Systems”
What do we want to do tonight ?
First:
• Get acquainted
• Review the syllabus
• Understand the scope, purpose, and expectations of the course
• Check out our textbook
• Visit our Laboratory (CP 206C)
What do we want to do tonight ?
Second:
• Discuss the levels of abstraction involved in a computer solution to problem
• Discuss major Numeric Data Types
• Develop expertise in working with various numeric bases
especially base 10, base 2 (binary), and base 16 (hexidecimal)
• Become proficient converting numbers from one base to another
• Become proficient with 2’s complement arithmetic
• Understand round off error, sign extension, and overflow
• Understand representation of floating point numbers
• Understand Binary Logic
The levels of abstraction involved in a computer solution to problem
• Problem
• Algorithm
• Language
• Machine Architecture
• Microarchitecture
• Circuits
• Devices
• Electron or light flow
Develop expertise in working with various numeric bases
• Base 10
• Base 2 (binary)
• Base 16 (hexidecimal)
• Others ?
Become proficient converting numbers from one base to another
• Base 10 to binary
• Binary to Base 10
• Base 10 to Hexidecimal
• Hexidecimal to Base 10
• Binary to Hexidecimal
• Hexidecimal to Binary
• Others ?
Major Numeric Data Types
• Unsigned Integers
• Signed Integers
• 2’s Complement Integers
• Floating Point Numbers
Become proficient with 2’s complement arithmetic
(See Text Figure 2.1)
• Signed Binary Integers
• 1’s Complement Binary Integers
• 2’s complement Binary Integers
Binary Arithmetic
• Unsigned add & subtract
• 2’s complement add & subtract
Understand round off error, sign extension, and overflow
• overflow
• sign extension
• Binary fractions (the binary point)
• round off error
Understanding Floating Point Numbers
IEEE Standard
• sign (1 bit), exponent (8 bits), fraction (23 bits)
(-1)**sign x 1.fraction x 2**exponent-127
1<= exponent <= 254
•
ASCII (7 bit or extended)
UNICODE (16 bit)
• See ASCII table
• See UNICODE table
Binary Logic
• NOT
• AND (NAND)
• OR (NOR)
• XOR
• Truth tables
• DeMorgan’s Theorem
What do we want to do tonight ?
(Second Week)
• Introduce logic circuits
• Relay logic
• MOS semiconductors
• Inverter
• Nor Gate
• Nand Gate
• Combinational Logic
• Decoders
• Full Adder
• PLA’s
• MUX – Multiplexers
What do we want to do tonight ?
•Storage Devices
• SR Flip-Flops
• D Latch Flip Flops
• J/K Master Slave Flip Flops
• Registers
• Addressing
• Finite State Machines
• State Diagrams
The LC-3
as a von
Neumann
machine
Data Path
of the LC-3
The Instruction
FORMAT
•Operation code
• Input Operand(s)
• Output Operand(s)
ADDRESSING MODES
• Register
•PC-relative
•Base + Offset (Base relative)
•Immediate
•Indirect
Operate Instructions
• Only three operations: ADD, AND, NOT
• Source and destination operands are
registers
ADD/AND (Register)
NOT (Register)
Note: Src and Dst
could be the same register.
ADD/AND (Immediate)
Note: Immediate field is
sign-extended.
Data Movement Instructions
•Load -- read data from memory to register
– LD: PC-relative mode [0010 DR PCoffset9]
– LDI: indirect mode
[1010 DR PCoffset9]
– LDR: base+offset mode [0110 DR BaseR offset6]
•Store -- write data from register to memory
– ST: PC-relative mode [0011 DR PCoffset9]
– STI: indirect mode
[1011 DR PCoffset9]
– STR: base+offset mode [0111 DR BaseR offset6]
•Load effective address – address saved in register
– LEA: immediate mode
[1110 DR PCoffset9]
LD (PC-Relative)
ST (PC-Relative)
LDI (Indirect)
STI (Indirect)
LDR (Base+Offset)
STR (Base+Offset)
LEA (Immediate)
Branch Instruction
BR
[0000 nzp PCoffset9]
• Branch specifies one or more condition codes
• If the set bit is specified, the branch is taken:
– PC is set to the address specified in the instruction
– Target address is made by adding SEXT(IR[8:0]) to
the PC
• If the branch is not taken:
- the next sequential instruction (PC) is executed.
BR
/////////////
/////
+
SEXT
Jump Instruction
JMP BaseR [1100 000 BaseR 000000]
• Jump is an unconditional branch -- always
taken.
• Base
– Address is contents of the register
– Allows any target address.
TRAP
• Calls a service routine, identified by 8-bit “trap vector.”
vector routine
x23
input a character from the keyboard
x21
output a character to the monitor
x25
halt the program
• When routine is done, PC is set to the instruction following
TRAP.
Example 1: Multiply
• This program multiplies two unsigned
integers in R4 and R5.
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
Yes
HALT
x3200
x3201
x3202
x3203
x3204
0101010010100000
0001010010000100
0001101101111111
0000011111111101
1111000000100101
R2 <- 0
R2 <- R2 + R4
R5 <- R5 – 1
BRzp x3201
HALT
Example
Address
Opcode
Instruction
Comments
x30F6
1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1
R1 PC – 3 = x30F4
x30F7
0 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0
R2 R1 + 14 = x3102
x30F8
0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1
M[PC - 5] R2
M[x30F4] x3102
x30F9
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2 0
x30FA
0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1
R2 R2 + 5 = 5
x30FB
0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0
M[R1+14] R2
M[x3102] 5
1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1
R3 M[M[x30F4]]
R3 M[x3102]
R3 5
x30FC
Using Branch Instructions
• Compute sum of 12 integers.
Numbers start at location x3100. Program starts at location x3000.
R1 x3100
R3 0
R2 12
R2=0?
YES
NO
R4
R3
R1
R2
M[R1]
R3+R4
R1+1
R2-1
Sample Program
Address
Instruction
Comments
x3000
1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0
R1 x3100
x3001
0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0
R3 0
x3002
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2 0
x3003
0 0 0 1 0 1 0 0 1 1 1 0 1 1 0 0
R2 12
x3004
0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1
If Z, goto x3009
x3005
0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0
Load next value to R4
x3006
0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1
Add to R3
x3007
0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1
Increment R1 (pointer)
X3008
0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1
Decrement R2 (counter)
x3009
0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0
Goto x3004
Flow Chart
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
NO
(R3 = M[x3012])
Print count
YES
Match?
NO
(TRAP x21)
(R1 ?= R0)
Input char
from keybd
(TRAP x23)
HALT
Incr Count
Load char from file
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
(TRAP x25)
Program (1 of 2)
Address
Instruction
Comments
x3000
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2 0 (counter)
x3001
0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0
R3 M[x3102] (ptr)
x3002
1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1
Input to R0 (TRAP x23)
x3003
0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0
R1 M[R3]
x3004
0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0
R4 R1 – 4 (EOT)
x3005
0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0
If Z, goto x300E
x3006
1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1
R1 NOT R1
x3007
0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1
R1 R1 + 1
X3008
0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0
R1 R1 + R0
x3009
0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1
If N or P, goto x300B
Program (2 of 2)
Address
Instruction
Comments
x300A
0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1
R2 R2 + 1
x300B
0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1
R3 R3 + 1
x300C
0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0
R1 M[R3]
x300D
0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0
Goto x3004
x300E
0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1
R0 M[x3013]
x300F
0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
R0 R0 + R2
x3010
1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1
Print R0 (TRAP x21)
x3011
1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1
HALT (TRAP x25)
X3012
Starting Address of File
x3013
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0
ASCII x30 (‘0’)
Filled arrow
= info to be processed.
Unfilled arrow
= control signal.
Three Basic Constructs
• There are three basic ways to decompose a task:
Task
True
False
Test
condition
Subtask 1
Test
condition
True
Subtask 1
Subtask 2
Subtask 2
Sequential
Subtask
Conditional
Iterative
False
LC-3 Assembly Language Syntax
•
•
•
Each line of a program is one of the following:
– an instruction
– an assember directive (or pseudo-op)
– a comment
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”) are also ignored.
•
An instruction has the following format:
LABEL OPCODE OPERANDS ; COMMENTS
optional
mandatory
An Assembly Language Program
•;
•; Program to multiply a number by the constant 6
•;
•
.ORIG x3050
•
LD
R1, SIX
•
LD
R2, NUMBER
•
AND
R3, R3, #0
; Clear R3. It will
•
; contain the product.
•; The inner loop
•;
•AGAIN ADD
R3, R3, R2
•
ADD
R1, R1, #-1 ; R1 keeps track of
•
BRp
AGAIN
; the iteration.
•;
•
HALT
•;
•NUMBER .BLKW 1
•SIX
.FILL x0006
•;
•
.END
Assembler Directives
• Pseudo-operations
– do not refer to operations executed by program
– used by assembler
– look like instruction, but “opcode” starts with dot
Opcode Operand
Meaning
.ORIG
starting address of program
address
.END
end of program
.BLKW
n
allocate n words of storage
.FILL
n
allocate one word, initialize with value n
.STRINGZ
n-character
string
allocate n+1 locations,
initialize w/characters and null
terminator
Trap Codes
•
LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don’t have to remember them.
Code
Equivalent
Description
HALT
TRAP x25
Halt execution and print message to console.
IN
TRAP x23
Print prompt on console,
read (and echo) one character from keybd.
Character stored in R0[7:0].
OUT
TRAP x21
Write one character (in R0[7:0]) to console.
GETC
TRAP x20
Read one character from keyboard.
Character stored in R0[7:0].
PUTS
TRAP x22
Write null-terminated string to console.
Address of string is in R0.
Sample Program
•
Count the occurrences of a character in a file.
Remember this?
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
NO
(R3 = M[x3012])
Print count
YES
Match?
NO
(TRAP x21)
(R1 ?= R0)
Input char
from keybd
(TRAP x23)
HALT
Incr Count
Load char from file
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
(TRAP x25)
Char Count in Assembly Language (1 of 3)
•;
•; Program to count occurrences of a character in a file.
•; Character to be input from the keyboard.
•; Result to be displayed on the monitor.
•; Program only works if no more than 9 occurrences are found.
•;
•;
•; Initialization
•;
•
.ORIG
x3000
•
AND
R2, R2, #0
; R2 is counter, initially 0
•
LD
R3, PTR
; R3 is pointer to characters
•
GETC
; R0 gets character input
•
LDR
R1, R3, #0
; R1 gets first character
•;
•; Test character for end of file
•;
•TEST
ADD
R4, R1, #-4
; Test for EOT (ASCII x04)
•
BRz
OUTPUT
; If done, prepare the output
Char Count in Assembly Language (2 of 3)
•;
•; Test character for match. If a match, increment count.
•;
•
NOT
R1, R1
•
ADD
R1, R1, R0
; If match, R1 = xFFFF
•
NOT
R1, R1
; If match, R1 = x0000
•
BRnp
GETCHAR
; If no match, do not increment
•
ADD
R2, R2, #1
•;
•; Get next character from file.
•;
•GETCHAR ADD
R3, R3, #1
; Point to next character.
•
LDR
R1, R3, #0
; R1 gets next char to test
•
BRnzp
TEST
•;
•; Output the count.
•;
•OUTPUT LD
R0, ASCII
; Load the ASCII template
•
ADD
R0, R0, R2
; Covert binary count to ASCII
•
OUT
; ASCII code in R0 is displayed.
•
HALT
; Halt machine
Char Count in Assembly Language (3 of 3)
•;
•; Storage for pointer and ASCII template
•;
•ASCII
.FILL
x0030
•PTR
.FILL
x4000
•
.END
Program to find number of negative numbers
1. Initialize Registers
2. Get next data
3. If sign is negative, increment count
4. Update data pointer
5. If not done, go to 2.
6. Store result
7. Halt
Program to find number of negative numbers
1.
Initialize Registers
R0 = Counter of neg numbers
R1 = Sign Mask
R2 = Number of Data words
R3 = Ptr to Data
R4 = Data buffer
2. Get next data
Load R4 with (R3)
3. If sign is negative, increment count
AND Sign Mask with Data, Inc R0 if not zero
4. Update data pointer
Inc R3
Dec R2
5. If not done, go to 2.
IF Positive go to 2.
6. Store result
Store R0 in location pointed to by result pointer
7. Halt
Program to find number of negative numbers
.ORIG x3000
;
; Initialze Registers
;
AND R0, R0, #0 ; Clr counter of negative numbers
LD R1, MASK
; Set sign bit mask
ADD R2, R0, #10; Set # of Data words
LD R3, PTR_DATA; Set Data Ptr
;
; Loop to check words
;
LOOP LDR R4, R3, #0 ; Fetch data word
AND R4, R4, R1 ; Test for sign bit
BRz NEXT
;
ADD R0, R0, #1 ; Inc counter if negative
NEXT ADD R3, R3, #1 ; Inc Data Ptr
ADD R2, R2, #-1
; Dec # of Data words left
BRp LOOP
;
; Store Result
;
STI R0, PTR_RESULT; Store result in 4010
HALT
;
; Data
;
MASK
.FILL x8000
PTR_DATA
.FILL x3020
PTR_RESULT .FILL x3040
.END
Assembly Language Instructions
1)
Move data Instructions
Reg Reg
ADD, AND
Reg Memory LD, LDR, LDI
Memory Reg ST, STR, STI
Clear REG
AND
LOAD ADDRESS
LEA
1)
Computation – Arithmetic and Logic
Arithmetic ADD
LOGIC
AND, NOT, (SHIFT)
2)
Program Control – support IF, WHEN, DO
BR
JMP
3)
Subroutines (& Methods) - Pass Parameters – by value, by reference,
Stack
JSR
JSRR
RET
4)
Trap – operating system support - Stack
TRAP
RET
5)
Interrupts – Status Reg / Hardware does the “call”.
Priorities, PSR, Supervisor Stack
RTI
Addressing
1)
Register – operand is in a register
2)
Immediate (or literal) – operand is in IR[5:0]
3)
PC Relative – operand is in PC+IR[8:0]ext or PC+IR[10:0]ext
4)
Base + Offset – operand is in BaseReg or BaseReg+IR[5:0]
5)
Indirect – operand address is at PC+IR[8:0]
Hardware
1)
Arithmetic and Logic Unit – Nominally 2 operands
2)
Memory - Memory Address Register (MAR) & Memory Data Register (MDR)
3)
Registers (R0-R7) – Convention R0, R5, R6, R7
4)
Program Counter (PC) – Incremented after instruction fetch!
4)
Instruction Register (IR) – Hold Instruction being executed
5)
Processor Status Register (PSR) – Privileged, Priority, CC’s
6)
Stack Pointer Storage Regs – Saved.SysStackPtr & Saved.UserStackPtr
7)
I/O Devices
Device Data Register(s)
Device Status Register(s) – Ready Bit, Intr Enable Bit, Priority?
Traps
1)
Execute
TRAP “vector”
-
Operating System Service Routines
2)
Trap Vectors are at memory locations [0000:00FF]
3)
Trap Vectors contain addresses of Trap Service Routines
4)
(PC) is loaded into R7
5)
Address of Trap Service Routine loaded into PC
6)
Service Routine Program executed
7)
Trap service routine program ends with an RET
( (R7) loaded into PC)
Subroutines
1)
Execute
JSR or JSRR
-
Call Subroutine or Method
2)
Location of Subroutine is “in” the Instruction
4)
(PC) stored in R7
5)
Address from JSR or JSRR is loaded into PC
6)
Subroutine is executed
R0 possibly contains passed parameter (or address)
Stack may contain passed parameters (or addresses)
R5 may be used to return error message
Ro possibly contains return parameter (or address)
Stack may contain return parameters (or addresses)
7)
Subroutine program ends with an RET
( (R7 loaded into PC)
How does this mechanism support recursion?
Interrupts
1)
Enable Interrupts by setting “intr enable” bit in Device Status Reg
2)
When device ready bit is set and the priority is higher than the priority of the
presently running program (and execution of an instruction is complete) the
processor initiates the interrupt
4)
The Processor saves the “state” of the program
The Processor goes into Privileged Mode
PSR bit 15 cleared
Priority level is set (likely established by the interrupting device)
The CC’s are cleared
(R6) is stored in USP.saved register
The Supervisor Stack Ptr is loaded into R6
The (PC) and the (PSR) are PUSHED onto the Supervisor Stack
The contents of the other registers are not saved. Why?
5)
The Processor Loads the PC from the Interrupt vector (vectors in 100:1FF)
6)
Interrupt Service Routine is executed
Ends with an RTI
7)
The stored user PSR, PC, and R6 are reloaded and the next instruction executed
(POPS top of stack into PC)
Higher Level Languages
High Level Languages give us:
•
Symbolic Names
•
Expressions
•
Libraries of functions/subroutines
•
Abstraction of underlying hardware
•
Readability
•
Structure – help keep bugs out
First C Program
/*
*
*
*
*
*
*/
Program Name : countdown, our first C program
Description : This program prompts the user to type in
a positive number and counts down from that number to 0,
displaying each number along the way.
/* The next two lines are preprocessor directives */
#include <stdio.h>
#define STOP 0
/* Function
: main
*/
/* Description : prompt for input, then display countdown */
int main()
{
/* Variable declarations */
int counter;
int startPoint;
/* Holds intermediate count values */
/* Starting point for count down
*/
/* Prompt the user for input */
printf("===== Countdown Program =====\n");
printf("Enter a positive integer: ");
scanf("%d", &startPoint);
/* Count down from the input number to 0 */
for (counter = startPoint; counter >= STOP; counter--)
{
printf("%d\n", counter);
}
}
return 0
Compiling C
Terms
•
Pre processor directives
#define
#include
•
Header Files
<stdio.h>
•
Data Types
int
char
double
•
Scope
•
Variable initiation
Local
Global
Scope
#include <stdio.h>
int globalVar = 2;
int main()
{
int localVar = 3;
/* This variable is global */
/* This variable is local to main */
printf("Global %d Local %d\n", globalVar, localVar);
{
}
int localVar = 4;
/* Local to this sub-block */
printf("Global %d Local %d\n", globalVar, localVar);
printf("Global %d Local %d\n", globalVar, localVar);
}
return 0
Another program
#include <stdio.h>
int main()
{
int amount;
int rate;
int time;
int hours;
int minutes;
int seconds;
/* The number of bytes to be transferred */
/* The average network transfer rate
*/
/* The time, in seconds, for the transfer */
/* The number of hours for the transfer
/* The number of mins for the transfer
/* The number of secs for the transfer
*/
*/
*/
/* Get input: number of bytes and network transfer rate */
printf("How many bytes of data to be transferred? ");
scanf("%d", &amount);
printf("What is the transfer rate (in bytes/sec)?
scanf("%d", &rate);
");
/* Calculate total time in seconds
time = amount / rate;
*/
/* Convert time into hours, minutes, seconds
hours = time / 3600;
/* 3600 seconds in an hour
minutes = (time % 3600) / 60; /* 60 seconds in a minute
seconds = ((time % 3600) % 60); /* remainder is seconds
*/
*/
*/
*/
/* Output results */
printf("Transfer Time : %dh %dm %ds\n", hours, minutes, seconds);
}
return 0
Another program
/* Include the standard I/O header file */
#include <stdio.h>
int inGlobal;
/* inGlobal is a global variable because */
/* it is declared outside of all blocks */
int main()
{
int inLocal;
/* inLocal, outLocalA, outLocalB are all */
int outLocalA; /* local to main
*/
int outLocalB;
/* Initialize */
inLocal = 5;
inGlobal = 3;
/* Perform calculations */
outLocalA = inLocal & ~inGlobal;
outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);
/* Print out results */
printf("outLocalA = %d, outLocalB = %d\n", outLocalA, outLocalB);
}
return 0
Another program
#include <stdio.h>
#define RADIUS 15.0
/* This value is in centimeters */
int main()
{
const double pi = 3.14159;
double area;
double circumference;
/* Calculations */
area = pi * RADIUS * RADIUS;
circumference = 2 * pi * RADIUS;
/* area = pi*r^2 */
/* circumference = */
/*
2*pi*r */
printf("Area of a circle with radius %f cm is %f cm^2\n",
RADIUS, area);
printf("Circumference of the circle is %f cm\n",
circumference);
return 0
}
Another program
int main()
{
char nextChar;
/* Next character in email address */
int gotAt = FALSE; /* Indicates if At @ was found */
int gotDot = FALSE; /* Indicates if Dot . was found */
printf("Enter your email address: ");
do {
scanf("%c", &nextChar);
if (nextChar == '@')
gotAt = TRUE;
if (nextChar == '.' && gotAt == TRUE)
gotDot = TRUE;
}
while (nextChar != ' ' && nextChar != '\n');
if (gotAt == TRUE && gotDot == TRUE)
printf("Your email address appears to be valid.\n");
else
printf("Your email address is not valid!\n");
return 0
}
Another program
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int main()
{
char nextChar;
/* Next character in email address */
int gotAt = FALSE; /* Indicates if At @ was found */
int gotDot = FALSE; /* Indicates if Dot . was found */
printf("Enter your email address: ");
do {
scanf("%c", &nextChar);
if (nextChar == '@')
gotAt = TRUE;
if (nextChar == '.' && gotAt == TRUE)
gotDot = TRUE;
}
while (nextChar != ' ' && nextChar != '\n');
if (gotAt == TRUE && gotDot == TRUE)
printf("Your email address appears to be valid.\n");
else
printf("Your email address is not valid!\n");
return 0
}
Another program
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int main()
{
char nextChar;
/* Next character in email address */
int gotAt = FALSE; /* Indicates if At @ was found */
int gotDot = FALSE; /* Indicates if Dot . was found */
printf("Enter your email address: ");
do {
scanf("%c", &nextChar);
if (nextChar == '@')
gotAt = TRUE;
if (nextChar == '.' && gotAt == TRUE)
gotDot = TRUE;
}
while (nextChar != ' ' && nextChar != '\n');
if (gotAt == TRUE && gotDot == TRUE)
printf("Your email address appears to be valid.\n");
else
printf("Your email address is not valid!\n");
return 0
}
Another program
; Program to compute z = x * Y
; (R5) -> X , (R5)-1 -> Y
;
AND
R0, R0, #0
; RO <= 0
LDR
LDR
BRz
BRp
R1, R5, #0
R2, R5, #-1
DONE
LOOP
;
;
;
;
load
load
if y
if y
value of x
value of y
is zero, we're done
is positive, start mult
; y is negative
NOT
ADD
R1, R1
R1, R1, #1
; R1 <= -x
NOT
ADD
R2, R2
R2, R2, #1
; R2 <= -y (-y is positive)
LOOP ADD
ADD
BRp
R0, R0, R1
R2, R2, #-1
LOOP
; Multiply loop
; The result is in R2
DONE STR
R0, R5, #-2
; z = x * Y