More on Assembly CS 210 Tutorial 4 Detail
More on Assembly CS 210 Tutorial 4
Detail of Echo program n n n entry main. enter; import ". . /IMPORT/callsys. h"; block main uses CALLSYS { code { public enter: { loop: ldiq $a 0, CALLSYS_GETCHAR; call_pal CALL_PAL_CALLSYS; blt $v 0, end; mov $v 0, $a 1; ldiq $a 0, CALLSYS_PUTCHAR; call_pal CALL_PAL_CALLSYS; br loop; end: n n n n n } { n n clr $a 1; ldiq $a 0, CALLSYS_EXIT; call_pal CALL_PAL_CALLSYS; n n n } n n n just specifes the entry point for the program (in other words, where the program starts executing). specify that the code in the specified file args) isofimported (included) just specify that we void are creating a block code called main, Like: public static main(€String[] using some definitions in a block called register that specifies the register numbers for the symbolic names a 0, a 1, v 0, etc, and using some definitions in a block called CALLSYS that specifies the values of CALL_PAL_CALLSYS, CALLSYS_GETCHAR, CALLSYS_PUTCHAR, etc. } code } block main
Detail of Echo program n n n n The lines code {………. . ………………. } code just specify that we are defining code (instructions), rather than data. The assembled bit patterns are placed in the section of memory used for code. The line public enter: labels some code, with the name “enter”. This code can be referred to outside the block, as main. enter.
Detail of Echo program: Label n n The lines { ¡ ¡ ¡ ¡ ¡ n n loop: ldiq $a 0, CALLSYS_GETCHAR; call_pal CALL_PAL_CALLSYS; blt $v 0, end; mov $v 0, $a 1; ldiq $a 0, CALLSYS_PUTCHAR; call_pal CALL_PAL_CALLSYS; br loop; end: } represent the real work.
Detail of Echo program n n n n The line ldiq $a 0, CALLSYS_GETCHAR; loads the constant value CALLSYS_GETCHAR (whatever that has been defined to be in the block CALLSYS, in “. . /IMPORT/callsys. h”) into register a 0. The line call_pal CALL_PAL_CALLSYS; then makes a request (rather like a function invocation) to the operating system to do something (read a character from the keyboard). The operating system uses the value in register $a 0 todetermine what action to perform (in this case read a character), and returns the result in register $v 0. Other parameters to system calls may be passed in registers $a 1, $a 2, $a 3, . . .
Detail of Echo program n n n The lines mov $v 0, $a 1; ldiq $a 0, CALLSYS_PUTCHAR; call_pal CALL_PAL_CALLSYS; move the character from register v 0 into register a 1, load the constant value CALLSYS_PUTCHAR into register $a 0 (to specify that the action is to write a character), and makes a request to the operating system (to write the character to the screen). The lines { clr $a 1; ldiq $a 0, CALLSYS_EXIT; call_pal CALL_PAL_CALLSYS; } Cause the program to terminate and control to be returned to the operating system.
Assembly instruction syntax n Ex: ¡ ¡ n addq $t 0, $t 1, $t 3; subq $t 0, 23, $t 4; There are five kinds of operands. The opcode determines the number and kind of legal operands: Register, Unsigned 8 bit constant, Register, Brand operation, Unsigned 26 bit constant. ¡ Register: The operand represents a source or destination register. It is written as “$register”, for example, $a 0, $v 0. ¡ Unsigned 8 bit constant: The second operand of an integer operate instruction can be of this form. The constant is written directly, without any additional annotation, for example 23 in the above subq instruction.
Assembly instruction syntax ¡ Memory address: The operand represents a memory address, computed as a displacement (offset) from a base register. It is written as “displacement($register)”, and means the displacement + contents of integer register $register. The displacement is a signed 16 bit integer. The displacement may be omitted if it is 0, allowing the notation “($register)”. If the register is $zero (register 31), which always contains 0, then the operand can be written with just the displacement. For example, we can write 24($t 0). The notation ($t 0) is an abbreviation for 0($t 0), and the notation 1234 is an abbreviation for 1234($zero). Displacement operands can only be used in load and store instructions. ¡ Branch destination: The operand represents a destination address for a branch instruction. It is written directly as the destination address, but is stored as a displacement from the address just after the branch instruction. The last two bits of the displacement are not stored in the instruction, because they are always 0. ¡ • Unsigned 26 bit constant: The operand of a special instruction is of this form. For example, PALL_PAL_CALLSYS in a call_pal instruction.
Load and store instructions n n n n To operate on memory values, we must first load the source data from memory, perform the computation, then store the result back in memory. opcode $reg. A, displacement($reg. B); Integer load instructions load a number of bytes starting at the specified address ldq (load quadword) loads the 8 bytes corresponding to a quadword, starting at the memory address, into register. ldbu (load byte unsigned) loads a single byte from the memory address, into the low byte of register, making the high 7 bytes zero. stq… stqu…
Run Data. Rep 2 n n n Exercise DATAREP 2 Suppose we have memory 0 x 1000000 0 x 123456789 abcdef 0 0 x 1000008 0 x 00000000 0 x 1000010 0 x 00000000 ldiq $t 0, 0 x 1000000; ldq $t 1, ($t 0); stb $t 1, 8($t 0); ldbu $t 2, 2($t 0); sll $t 2, 56, $t 3; sra $t 3, 56, $t 4; stq $t 4, 16($t 0);
- Slides: 10