An “interactive” application An introduction to “structured” assembly language programming

advertisement
An “interactive” application
An introduction to “structured”
assembly language programming
A human-computer dialogue
• Typical pattern:
– Computer asks a question
– Human types in a response
• Simple example:
– Computer says: How many dots?
– Human replies: 125 <ENTER>
– Computer does as requested.
Standard C functions
•
•
•
•
How can we do input/output in assembly?
We can call standard C library functions
But we need to know their “prototypes”
For output (where STDOUT equals 1):
write( STDOUT, &query, sizeof( query ) );
• For input (where STDIN equals 0):
read( STDIN, &buffer, sizeof( buffer ) );
Structured programming
•
•
•
•
•
A discipline for faster program design
Idea: break a big task into simple pieces
It’s known as “task decomposition”
We can use a diagram to illustrate it
Diagram is called a “Structure Chart”
Structure Chart example
main
obtain_input
process_data
print_output
Code for the ‘main’ function
main:
.section
call
call
call
ret
.text
obtain_input
process_data
print_output
.globl
main
Stubs
• You can write empty ‘stubs’ (for testing)
obtain_input:
ret
process_data:
ret
print_output:
ret
• Now you can ‘test’ your skeleton program
(e.g. assemble, link, and execute)
Add details for each ‘stub’
• First write your final subroutine, so you
can see “something” on the screen
• You can use ‘dummy’ data temporarily
• Get it working correctly (debugged)
• Then you can focus on you next ‘stub’
Main algorithm
•
•
•
•
•
•
•
Converting user’s input-string to a number
That is “the hard part” in this example
Idea: “scan” the array of character-codes
Test each code (to see if it’s a valid digit)
It must lie between ‘0’ and ‘9’ (ascii codes)
If code is not not valid, the scanning stops
Otherwise, adjust the accumulated total
Some new directives
.ascii
.asciz
.long
.byte
.space
Some new CPU instructions
sub
dec
cmp
jg
jl
jz
jnz
movzx
loop
imul
Exercise with ASCII codes
• See what happens if you change the ascii
code used for as value of the ‘dot’ variable
• Try using ascii value 7
• Try using ascii value 9
• Try using ascii value 10
• Try using ascii value 111
• Try using ascii value 250
Download