Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

program for LC3 Assembly language

How do you convert any character input from the user to its corresponding decimal value? I was just having trouble getting started.

The program has to achieve the following things:

  1. The program accepts character from keyboard.

  2. If the character is a digit (‘0’ through ‘9’): a) Convert the character to its corresponding decimal value. In other words, ‘0’ becomes zero, ‘1’ becomes 1, ... ‘9’ becomes 9. Let’s call that value R (for “run length”). b) Wait for another character (using GETC). c) Print R copies of that character to the console. ) d) Go back to Step 1.

  3. Else, if the character is Enter/Return (ASCII #10): Print a linefeed (ASCII #10) to the console, and go back to Step 1.

  4. Else, if the character is anything else, halt the program.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You convert decimal digit character it to number subtracting '0' (=0x30) from it. For hex digits ('A'to 'F'): If character is greater than '@', you subtract 0x37 from it ('A' -> 0x0a). For hex digits ('a'to 'f'): If the value is still bigger than 15, you subtract 0x20 from it Or you can use a table for mapping. 256 bytes is not bery big table.

  • You set result (variable, register, ...) to zero
  • You read character by character in a loop
  • You convert the character to new number (of one digit)
  • if it's invalid hex digit character, return the variable - you're done
  • else variable = variable * 16 + new number
  • multiplying by 16 can be done by shifting left 4 bit places

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...