close

Вход

Забыли?

вход по аккаунту

код для вставкиСкачать
About “problem-solving”
How a fresh way of thinking about
a programming question can
reveal a solution already at hand!
Old problem
• In our ‘reverse.s’ demo-program we saw
how we could use the x86 ‘push’ and ‘pop’
instructions to rearrange the order of the
characters in a memory-buffer
• Our technique used two parameters:
– the buffer’s base-address (i.e., a ‘pointer’)
– the number of characters (i.e., a ‘counter’)
A ‘pointer’ and a ‘counter’
“Please type a sentence on the line below:”
inbuf:
‘H’ ‘e’
count:
‘l’
‘l’
‘o’
‘,’
‘‘
‘w’ ‘o’
‘r’
‘l’
‘d’
‘.’
‘\n’
13
“OK, here is what you typed in backward order:”
inbuf:
‘.’
‘d’
‘l’
‘r’
‘o’ ‘w’
‘‘
‘,’
‘o’
‘l’
‘l’
‘e’ ‘H’ ‘\n’
New problem
• We can apply our solution to that previous
problem in order to produce a solution to
our current problem
• We need to regard the newer problem as
comprising several cases of the older one!
• We want to reverse the order of characters
in the individual ‘words’ instead of doing it
for the entire ‘sentence’
Two ‘pointers’ and two ‘counters’
“Please type a sentence on the line below:”
inbuf:
‘H’ ‘e’
‘l’
‘l’
‘o’
‘,’
‘‘
‘w’ ‘o’
5
‘r’
‘l’
‘d’
‘.’
‘\n’
5
“OK, here each word you typed is in backward order:”
inbuf:
‘o’
‘l’
‘l’
‘e’ ‘H’
‘,’
‘‘
‘d’
‘l’
‘r’
‘o’ ‘w’
‘,’
‘\n’
What do we need?
• So the foregoing insight shows us what we
need in order to reduce our newer problem
to several cases of our older problem:
– How can we get a ‘pointer’ to the beginning
character of each word?
– How can we get the ‘count’ of the characters
which are part of that word?
Invent a ‘recognizer’
• We now see the usefulness of a routine
that would be able to recognize when a
character is part of a ‘word’ -- and when
it’s not part of a word!
• Side-issue: What about digit-characters?
• Well, sometimes digit-characters do get
typed as part of words in a sentence:
– e.g., the lovable ‘R2D2’ robot in ‘Star Wars’
– or important years (like ‘1492’) in history
‘isalphanumeric’
• These considerations suggest to us that
we ought to design a subroutine which
tells us, yes or no, if a character is some
letter in the alphabet or a numerical digit,
and on the other hand tells us it’s not an
‘alphanumeric’ character
• We put an assembly language function
which does that on our CS210 website
1/--страниц
Пожаловаться на содержимое документа