ECE3120: Computer Systems Chapter 4: Advanced Assembly Programming - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

ECE3120: Computer Systems Chapter 4: Advanced Assembly Programming

Description:

The reception of an ASCII carriage return ($0D) terminates the reception of ... are already eight prime numbers in the current line, then also output a carriage ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 19
Provided by: xubi
Category:

less

Transcript and Presenter's Notes

Title: ECE3120: Computer Systems Chapter 4: Advanced Assembly Programming


1
ECE3120 Computer SystemsChapter 4 Advanced
Assembly Programming
  • Dr. Xubin He
  • http//iweb.tntech.edu/hexb
  • Email hexb_at_tntech.edu
  • Tel 931-3723462, Brown Hall 319

2
  • Prev
  • Subroutine Examples
  • Today
  • Using D-Bug12 to perform I/O operations

3
Using the D-Bug12 Functions to Perform I/O
Operations
4
- All functions listed in Table 4.2 are written
in C language. - The first parameter to the
function must be passed in accumulator D.
The remaining parameters must be pushed onto the
stack in the reverse order they are listed in
the function declaration. - Parameters of type
char will occupy the lower order byte of a word
pushed onto the stack. - Parameters pushed onto
the stack before the function is called remain on
the stack when the function returns. It is the
responsibility of the caller to remove passed
parameters from the stack. - All 8- and 16-bit
values are returned in accumulator D. A returned
value of type char is returned in accumulator
B. int getchar (void) Pointer
address EE84 Function retrieve a single
character from the control terminal. Incoming
parameter none Returned value 8-bit
character in accumulator B
5
Adding the following instruction sequence will
read a character from SCI0 port getchar equ E
E84 jsr getchar,PCR int
putchar(int) Pointer address EE86 Function
outputs a single charactre to the control
terminal Incoming parameter character to be
output in accumulator B Returned vale the
character that was sent (in B) - This function
outputs a character to serial communication port
SCI0. - The character to be output should be
placed in accumulator B. - The following
instruction sequence will output the character A
to serial port SCI0 when the program is running
on the Axiom CME-12BC demo board. putchar equ
EE86 ldab 41 place the ASCII code of
A in accumulator B jsr putchar,PCR
6
int printf(char format,) Pointer address
EE88 Function cobvert, format, and print its
arguments on the standard output. Incoming
parameters zero or more integer data to be
output on the stack, D contains the address
of the format string. The format string must
be terminated with a zero. Returned value
number of characters printed in D. - This
function is used to convert, format, and print
its argument as standard output under the
control of the format string pointed to by
format. - All except floating-point data types
are supported. - The format string contains two
basic types of objects 1. ASCII characters
which will be copied directly to the display
device. 2. Conversion specifications. Each
conversion specification begins with a percent
sign (). 3. Optional formatting characters may
appear between the percent sign and ends with a
single conversion character in the following
order -ltFieldWidthgt.ltPrecisiongth
l
7
The Meaning of Optional Characters
8
Formatting Characters Supported by the printf()
function
Example for outputting a message (Flight
simulation) CR equ 0D LF equ 0A printf equ
EE88 ldd prompt jsr printf,PCR pro
mpt db Flight simulation,CR,LF,0
9
Example for outputting three numbers m, n, and
gcd along with a message CR equ 0D LF equ 0A
printf equ EE88 ldd gcd pshd ldd n
pshd ldd m pshd ldd prompt jsr printf,PC
R leas 6,sp prompt db The greatest
common divisor of d and d is d,CR,LF,0
10
int far GetCmdLine(char CmdLineStr, int
CmdLineLen) Pointer address EE8A Incoming
parameters a pointer to the buffer where the
input string is to be stored and the maximum
number of characters that will be accepted
by this function. Returned value a string from
the user (usually from the keyboard) - This
function is used to obtain a line of input from
the user. - The reception of an ASCII carriage
return (0D) terminates the reception
of characters from the user. - The caller of
this function normally would output a message so
that the user knows to enter a message. The
example in the next page illustrates
this interaction.
11
printf equ EE88 GetCmdLine equ EE8A cmdlinelen e
qu 100 CR equ 0D LF equ 0A prompt db Please
enter a string ,CR,LF,0 inbuf rmb 100 ldd
prompt output a prompt to remind the user to
jsr printf,PCR enter a string ldd cmdlinel
en push the CmdLineLen pshd ldd inbuf
call GetCmdLine,PCR read a string from the
keyboard puld clean up the stack
12
Example 4.15 Write a program that invokes
appropriate functions to find the prime number
between 100 and 1000. Output eight prime numbers
in one line. To do this, you will need to   1.
 write a subroutine to test if an integer is a
prime. 2. invoke the printf() function to
output the prime number. 3. write a loop to
test all the integers between 100 and
1000. Solution The logic structure of the
program is Step 1 Output the message The prime
numbers between 100 and 1000 are as
follows. Step 2 For every number between 100
and 1000, do the following   1. call the
test_prime() function to see if it is a
prime. 2. output the number (call printf()) if
it is a prime. 3. if there are already eight
prime numbers in the current line, then also
output a carriage return.
13
The algorithm of the function test_prime() Step
1 Let num, i, and isprime represent the number to
be tested, the loop index, and the flag to
indicate if num is prime. Step 2 isprime
0 Step 3 For i 2 to num/2, do the
following if num i 0 then return isprime
1 return The stack frame of the function
test_prime()
14
CR equ 0D LF equ 0A i equ 0 distance of
variable i from stack top ilimit equ 2 distance
of variable ilimit from stack top prime_flag equ 4
distance of variable isprime from stack
top num equ 9 distance of variable num from the
stack top plocal equ 5 number of bytes
allocated to local variables upper equ 1000
upper limit for testing prime lower equ 100
lower limit for testing prime printf equ EE88
location where the address of printf() is
stored   org 800 out_buf rmb 10 prime_cnt rmb 1
k rmb 2 temp rmb 2 org 1000 ldx upper stx tem
p pshx ldx lower stx k initialize k to 100
for prime testing pshx
15
ldd format0 jsr printf,PCR leas 4,sp clr pr
ime_cnt again ldd k cpd upper bhi stop stop
when k is greater than upper pshd jsr prime_tst
test if k is prime leas 2,sp deallocate
space used by outgoing parameters tstb beq next
_k test next integer if k is not
prime inc prime_cnt increment the prime
count ldd k pshd ldd format1 jsr printf,PCR
output k leas 2,sp ldaa prime_cnt cmpa 8
are there 8 prime numbers in the current
line? blo next_k output a CR, LF if there are
already 8 prime numbers in the current line
16
ldd format2 jsr printf,PCR clr prime_cnt ld
x k inx stx k bra again next_k ldx k inx stx
k jmp again stop swi prime_tst pshx leas -plocal
,sp allocate local variable ldaa 1 staa prime
_flag,sp set the flag to true ldd num,sp cpd
1 beq not_prime 1 is not a prime
number cpd 2 beq is_prime 2 is a prime
number lsrd compute num/2 as the test
limit std ilimit,sp "
17
ldd 2 std i,sp set first test number to
2 test_loop ldx i,sp ldd num,sp idiv cpd 0
if num can be divided by i, then beq not_prime
it is not a prime ldd i,sp cpd ilimit,sp beq is
_prime num is prime at the end of test
loop ldx i,sp inx stx i,sp bra test_loop not_
prime clr prime_flag,sp is_prime ldab prime_flag,s
p put the result in B leas plocal,sp pulx rts
formmat0 db CR,LF,"The prime numbers
between d and d are as follows
db ",CR,LF,CR,LF,0 format1 db " d
",0 format2 db " ",CR,LF,0 end
18
Next
  • Chapter 7 Parallel I/O Interfacing
Write a Comment
User Comments (0)
About PowerShow.com