简体   繁体   中英

How to develop an intuition to code in LC-3 Assembly?

I read about how LC-3 works, but I can't for the life of me figure out how to code in LC-3 assembly. My goal is to be able to write simple programs like generating Fibonacci numbers or sorting an array.

Can someone point to me to resources to learn this? I'm fluent in Python and Java, so the underlying logic behind those problems is clear to me.

There are several aspects to learning assembly language, which is the human readable version of the machine code of the processor.

Basically other languages are at a logical level, whereas machine code is very much at a physical level

  • For one, this goes particularly to the difference in the notions of storage:

    • Variables, which are logical vs. CPU registers and memory which are physical
    • Logical variables are dynamic, CPU registers and memory are fixed, permanent
    • Variables, which have types vs. physical storage having bits

    So, when we write assembly language, we translate our pseudo code: logical code with lots of typed variables of limited lifetime, in part by mapping logical variables onto the fixed physical resources. There are often more variables than CPU registers, especially when some of the registers have dedicated purpose, like stack or return address.

  • For another, today's other languages generally employ structured programming, whereas in assembly language/machine code we have if-goto-label.

    All structured statements have translation in if-goto-label. Each translation is a transformation of a pattern in structured form to a pattern in if-goto-label form. Follow the patterns properly and you'll reproduce the control flow of your pseudo code — very easy to take short cuts here and make confusing mistakes, so I encourage a methodical approach here.

  • Other languages have rich expressions: having operators of many levels of precedence, and as complex as you like using () 's. Machine code has instructions that take (usually) at most 3 operands.

  • Function calls, stack frames, parameter passing, return values are a fairly deep subject, function prologue & epilogue.

    • Parameters need to be placed into known locations by the caller and found from those locations by the callee
    • The fixed physical registers need to be shared between caller and callee, so there is a protocol for doing that sharing. Registers are either call preserved or call clobbered — each group is appropriate for a different scenario, and has its own rules/requirements in order to work properly.
    • There can be an explicit call stack with stack pointer in assembly language that we don't see in C code.
    • There can be an explicit return address, which should be thought of as a parameter, that the callee uses to return to the proper caller (as that can be different dynamically).
    • Return values are placed into known locations by the callee and found there by the caller upon return.
    • Saving call-preserved registers, local variable storage eg for arrays, and parameters (including the return address) and local variables can be live across a function call — all these things need memory storage, usually in the form of some space allocated on the stack (though sometimes on LC-3 these are done as global variables, which means recursion is not supported). If stack space is used for any of these, that space is called a stack frame.
      • Stack space is allocated in function prologue and released in function epilogue — these are sections of the function code that precede and follow the function body (they are executed only once per function invocation, they are never part of loops even if the entire body of the function is a loop).
    • See more information by looking for the "calling convention" you're using, which will describe the register sharing groups, dedicated registers (eg stack pointer), and parameter and return value locations.
    • An analysis of whether a logical variable is "live across a call" helps to choose the an appropriate CPU register and tells us whether we need to save that register in prologue and restore it in epilogue.

For more information, see some of following resources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM