Assembly Language Flashcards

1
Q

Machine language

A

○ bit patterns that are directly executable by computer
○ May look like gibberish to humans but it is actually the binary encoded instructions that the CPU can understand.
○ May also contain data in addition to instructions.
○ Assembled into files called objects, binaries, executables, which specify how the machine memory should be set up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assembly language

A

○ symbolic representation of machine language
○ Instructions as mnemonic ASCII strings e.g. ADD R2, R6, R2
○ Cannot run but mostly readable by humans
○ Can be handwritten or produced by a compiler from a high level language (like C)
○ Assembly files can also contain ASCII labels e.g. BRnzp LOOP
○ ISA dependent. (High level languages usually ISA independent). Usually the case that each ISA has only one assembly language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assembler

A

used to translate assembly programs (.ASM) into machine code (.OBJ)
object file that is a specification for how the machine memory should be set up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Linking

A

process of connecting several .OBJ files together into one executable program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Programming:

A

process of designing/writing/testing/debugging/maintaining the “source code” of computer programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Conditional control

A

a way for our program to change the natural flow of a program based on a condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Subroutine

A

group of instructions meant to perform a specific task e.g. square a number, print out a string, etc. in assembly; similar to a “function” in a high level language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Pointers

A

variable storing a memory address (as opposed to a regular variable that holds data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dereferencing

A

act of using the memory address held onto by a

pointer to read/write from the memory location held onto by that pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Assembly directives

A

provide an indication to the assembler of where it should place various blocks of code or data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does assembler program operate

A

2 Phases:
First Phase: convert labels into offsets and remove comments
Second Phase: Converts assembly code into machine code. Uses ISA to do this and saves it in .obj file
After assembler finishes, a loader program load it into computer memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Partitioning of LC4 Memory

A
User Region:
- Programs run by user as excel
Processes run in user Mode (PSR[15]=0) are not allowed to access OS location in memory
- User Code: x0000 - x1FFF
- User Data: x2000 - x7FFF

OS region:

  • Processes run in OS mode with PSR[15]=1
  • OS Code: x8000 - x9FFF. However, 1st address in OS is at x8200. Bw. x8000-x8200 -> TRAP Table
  • OS Data + Device Memory: xA000 - xFFFF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.DATA

A

Next values are in data memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

.ADDR

A

Set current address to the specified value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

.FILL IMM16

A

Set value at the current address to the specified 16-bit value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Loader Program

A

The object file specifies initial values for data memory locations. The loader program in the simulator takes this object file and configures the LC-4 memory according to these specifications.

17
Q

Assembly Directives

A

.Data, .Code, . ADDR, . FILL IMM16
provide an indication to the assembler of where it should place various blocks of code or data
are NOT INSTRUCTIONS

18
Q

Assembly files are stored in which format

A

Stored as text files. Text files consist of ASCII characters only

19
Q

Code for small OS

A
.CODE
.ADDR x8200
.FALIGN
 CONST R7, #0
 RTI -> downgrade to user mode and set PC =R7
20
Q

Advantage of Immediate addressing Mode instructions

A

Can bypass storing data first in register and specifying register address in instruction and instead can directly add data to instruction

21
Q

TRAP

A

Changes the PC to a memory address that is part of the OS. So that the OS will perform some task in behalf of the program that is being run. ‘Service Call’.
Once OS is finished performing service call, the PC is set to the address of the instruction one after the TRAP call

22
Q

Difference BRnzp vs. JMP

A

JMP has a wider range than using BRnzp to Jump

23
Q

Outline for enabling, calling and returning from subroutine

A
  1. Give subroutine a unique name using a Label
  2. Ensure subroutine is loaded at memory address that is multiple of 16 -> .FALIGN
  3. Pass in argument using register file
  4. Call subroutine using JSR
  5. Return data using register file
  6. Return from subroutine using RET.

–> wrap subroutine into JMP so that it is not run by accident

24
Q

Pointer

A

Address variable. Variable holding memory address

25
Q

Dereferencing the pointer

A

act of using a pointer value to read from or write to a memory location

26
Q

Assembly Program

A

Text File of form .asm that specifies the instructions and data needed for our program.
File may contain: instructions sequences, data blocks, labels, directives, pseudo instructions

27
Q

JSR Instruction

A

R7 = PC+1 , PC = (PC& x8000) | (IMM11 &laquo_space;4)
If MSB = 1 of current PC. Can only jump to addresses in OS Space. Because, PC& x8000 will yield x8000. I.e. MSB stays at 1 and with (IMM11 &laquo_space;4) I can max produce a 15bit and not a 16bit number. Numbers I can produce with IMM11 &laquo_space;4: 0, 16, 32, …
Please note subroutine could also come before actual code and I can jump back to the address
if MSB = 0 of current PC. Can only jump to following addresses in User Space.

Safety messure, when in user space can only access user code. MSB = 0

Need to use .FALIGN prior to subroutine code because left shift by 4 bits, implies that with JSR I can only jump to addresses that are a multiple of 16

Shift is done to give JSR a wider range of addresses it can cover

28
Q

What does a loader do

A

takes .obj file and configures memory according to the specification in the file

29
Q

Labels

A

Must be unique. code might still assemble but results may be unpredictable

30
Q

.STRINGZ

A

Directive
initialize a sequence of n+1 memory locations
Fill the first n words of memroy with n-long argument provided in double quotation marks.
Set final word of memory (n+1) to x0000 (denotes end of string

e.g.
.DATA
.ADDR x2020
.STRINGZ “hello”

31
Q

2 ways to set a pointer

A
  1. Use CONST and HICONST

2. Use Label + LEA

32
Q

user_start assembly code

A

.DATA
.ADDR x2000
USER_STACK_ADDR .UCONST x7FFF ; address where stack should start for users

.CODE ; adddress where USER_START should be loaded
.ADDR x0000

.FALIGN
USER_START
LC R6, USER_STACK_ADDR ; initialize the stack pointer (R6)
ADD R5, R6, #0 ; initialize the frame pointer (R5)
LEA R7, main
JSRR R7 ; invoke the main routine
END

33
Q

“wrapping” a subroutine

A

that the C-compiler only generates JSR’s and really has no mechanism to generate “TRAP” instructions from your C code.
We need to make our own Assembly file of special subroutines that call the TRAPs in our OS.
The c files than calls this function/wrappers, which have the respective label in our own Assembly file.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; TRAP_PUTC Wrapper ;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.FALIGN
lc4_putc

;; PROLOGUE ;;
STR R7, R6, #-2 ;Store Main Return Address
STR R5, R6, #-3 ;Store Callers (Main) Frame Pointer
ADD R6, R6, #-3 ;Update Stack Pointer
ADD R5, R6, #0 ;Update LC4_PUTC Frame Pointer

;; FUNCTION BODY ;;
; get arguments to the trap from the stack and copy them to the register file for the TRAP call
LDR R0, R5, #3 ;Load Argument Value from Stack to R0

TRAP x01 ; R0 must be set before TRAP_PUTC is called

; TRAP_PUTC has no return value, so nothing to copy back to stack
CONST R7 #0 ; not necessary - no return value value to save, however creating “fake” 0 return value in R7 just to keep Epilogue the same

;; EPILOGUE ;;

ADD R6, R5, #0 ; not necessary - no local variables in lc4_putc to pop, however adding line just to keep epilogue template the same
ADD R6, R6, #3 ; decrease stack, remove return adress, value, pointer etc.
STR R7, R6, #-1 ; not necessary - as lc4_putc has no return value, update return value with “fake” 0 Return Value, to keep epilogue the same
LDR R5, R6, #-3 ; restore main frame pointer
LDR R7, R6, #-2 ; restore R7 to return to +1 after JSR LC4_PUTC call

RET