5

In the systems software course that I have this semester, we are being taught assemblers and other system software. While reading across the course I came across the topic of LITERALS.

There was a comparison between literals and immediate operands that said that the only difference between them is that literals are not assembled as a part of the instruction, whereas immediate operands are.

Why must we use literals if we can use immediate operands? What is it that makes them different? In other words in when to use literals and when to use immediate operands?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Crystal Meth
  • 589
  • 1
  • 4
  • 16
  • 3
    A processor doesn't know anything about literals, it only supports immediate operands and pointers to memory. Literals are a feature of an assembler or compiler. Exactly how a literal value is used in an executable program is determined by the context in which you use it. – Hans Passant Mar 05 '14 at 17:34

1 Answers1

10

Immediate operands are literal values you can encode into the instructions themselves, e.g.,

          MOV   R1,  17  ; move 17 as a value to R1

But, you may have need to put literal values into data structures or tables that your program may use. You use assembler "psuedo-ops" that declare storage to do this:

          DW      17     ; define a word containing the literal 17

Some literals, notably text strings, almost never fit into the immediate field (not enough bits in the immediate field), so really cannot be included in your program as instruction-immediate values:

    XYZ    DC       "I am a duck."

When this happens, you will typically find instructions that refer to the declared data via its label as an implicit immediate value, which is not a literal:

           MOV    R1, XYZ

An exception is an extremely short string literal:

           MOV    R1,  "A"

This isn't really different than the implicit literal in a call statement:

           CALL   FOO

which refers to the label on the FOO code:

     FOO:  MOV   R1, 17
           RETURN

Your assembler syntax may vary from this, but the principles stay the same.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • So, this means that the advantage of using literals is that they are capable of storing larger data like strings which cannot be stored in immediate operands? – Crystal Meth Mar 05 '14 at 17:34
  • 8
    "Literals" are character sequences that self-identify their value, e.g, 17 and "abc". Immediate operands are ways of embedding literal values directly into machine instructions. When the literal value won't fit, and you need it, you have to put it somewhere else (e.g, in a DC) and refer to it using the address of that somewhere else (usually a label), and you can often do that by writing the name of the label in the place where an immediate value is allowed in an instruction. – Ira Baxter Mar 05 '14 at 17:41