Generalities for Assembly Language

Slides:



Advertisements
Similar presentations
Catch up on previous topics Summary and putting together first four classes.
Advertisements

Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
SPIM and MIPS programming
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
MIPS Function Continued
MIPS Assembly Language Programming
Ch. 8 Functions.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
83 Assembly Language Readings: Chapter 2 ( , 2.8, 2.9, 2.13, 2.15), Appendix A.10 Assembly language Simple, regular instructions – building blocks.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Registers and MAL Lecture 12. The MAL Architecture MAL is a load/store architecture. MAL supports only those addressing modes supported by the MIPS RISC.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming.
True Assembly Language Part I. The Assembly Process The process of translating a MAL code (an assembly language program) into machine code (a sequence.
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
MIPS Assembly Language Chapter 15 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
C is a high level language (HLL)
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 312 Computer Architecture & Organization
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Computer Architecture & Operations I
Computer Organization and Design Assembly & Simulation
Lecture 4: MIPS Instruction Set
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
Assembly Programming using MIPS R3000 CPU
Adventures in Assembly Land
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Control Flow and Arrays
MPIS Instructions Functionalities of instructions Instruction format
ECE232: Hardware Organization and Design
Computer Organization and Design Assembly & Simulation
Instruction encoding The ISA defines Format = Encoding
Review.
MIPS function continued
Computer Instructions
Flow of Control -- Conditional branch instructions
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
Assembly and Simulation
MIPS Instruction Set Architecture
Assembly Programming using MIPS R3000 CPU
MIPS Assembly.
Instruction encoding The ISA defines Format = Encoding
CS352H Computer Systems Architecture
Adventures in Assembly Land
MIPS Assembly Language Programming Computer Architecture
Adventures in Assembly Land
9/27: Lecture Topics Memory Data transfer instructions
MIPS Arithmetic and Logic Instructions
CS 286 Computer Architecture & Organization
Control Flow and Arrays
Presentation transcript:

Generalities for Assembly Language 1 item per line of source code directive (declaration) instruction Comments are always allowed, but the syntax varies from one architecture to another Macro substitution like the #define from C no macros in MIPS assembly language!

Comments in MIPS assembly language # Here is my comment. # NO spanning lines! # Everything to the right of the # pound character to the end of the # line is a comment.

Declarations The format of the syntax: [label] type [initial value] type is one of .byte for a single, character-sized variable .word for a single, integer-sized variable .float for a single-precision-sized variable shows optional item

Declaration Examples: a_char: .byte 'a' # single quotes count: .word MAX: .word 1000 # decimal value mask: .word 0x34006700 # hexadecimal X: .word # initial value of 0 char: .byte # initial value is # NULL character .word # initial value is 0, # but how to access? e: .float 2.71828 # IEEE single # precision

More Declaration Examples: # an array of 5 characters, initialized letters: .byte 'a', 'b', 'c', 'd', 'e' # 2 labels for the exact same variable count1: count2: .word 0 # letter case is distinguished! # 2 separate variables are declared MAX: .word 1000 max: .word 6

Yet More Declaration Examples: # strings go in double quote marks err_msg: .asciiz "integer too large\n" no_null: .ascii "non null terminated" xyznull: .asciiz "xyz" # 4 chars xyz: .ascii "xyz" # 3 chars

Directives A way to give information to the assembler. All directives start with '.' (on many architectures). MIPS examples: .byte # space for 1 character-sized # variable .word # space for 1 integer-sized .float # space for 1 single precision- # sized variable

More Directives Examples .data # identifies the start of the # declaration section # There can be more than 1 .data # section in a program. # There will be 1 global section # where all data is placed. .text # identifies the start of a section # of code # There can be more than 1 .text # section in a program.

Yet More Directives Examples .asciiz # Places a null-terminated string # into memory. # A string consists of # consecutively allocated # characters. .ascii # Places a string into memory, # without null termination.

MIPS R2000 register usage syntax: $regnumber examples: $3, $18, $20 all 32 registers are both general purpose, yet have specific uses. . . $0 always the value 0 $1 used by the assembler for MAL->TAL translation $2, $3 function return values $4 - $7 parameters $26, $27 used by the operating system $29 stack pointer $31 return address register 10

Which registers to use? $0 for the immediate value 0 $8 - $25 for all variables When writing code, use registers as needed. Karen’s most excellent advice: Document what is in each register as you write code (not after the code is completed) ! 11

d must designate a register s1, s2 may designate a register or be an immediate r1, r2 must designate a register Only 1 immediate operand per instruction! (for common sense reasons) 3

Some arithmetic/logical instructions move d, s1 d = s1 add d, s1, s2 d = s1 + s2; two's complement addu d = s1 + s2; unsigned sub d = s1 – s2; two's complement subu d = s1 – s2; unsigned mul d = s1 * s2; two's complement div d = s1 / s2; two's complement; gives quotient divu d = s1 / s2; unsigned; gives quotient

More arithmetic/logical instructions rem d, s1, s2 d = s1 / s2; two's complement; gives remainder remu d = s1 / s2; unsigned; gives remainder and d = s1 & s2; bitwise AND or d = s1 | s2; bitwise OR not d, s1 d = ~s1; bitwise complement nand d = s1 NAND s2; no C equivalent nor d = s1 NOR s2; no C equivalent xor d = s1 ^ s2; bitwise XOR

More arithmetic/logical instructions rol d, s1, s2 d = rotate left of s1 by s2 places ror d = rotate right of s1 by s2 places sll d = logical left shift of s1 by s2 places sra d = arithmetic right shift of s1 by s2 places srl d = logical right shift of s1 by s2 places

#C code # # aa = bb + cc + (dd – ee); 16

MIPS Load/Store Instructions mnemonic operands operation lw d, addr one word is loaded from addr and placed into d; addr must be word aligned lb d, addr one byte is loaded from addr and placed into the rightmost byte of d; sign extension defines the other bits of d 4

more MIPS Load/Store Instructions mnemonic operands operation lbu d, addr one byte is loaded from addr and placed into the rightmost byte of d; zero extension defines the other bits of d li d, immed the immediate value is placed into d sw d, addr a word in d is stored to addr; addr must be word aligned 18

more MIPS Load/Store Instructions mnemonic operands operation sb d, addr the byte in the rightmost byte of d is stored to addr la d, label the address assigned for label is placed into d 19

loads l_ $_, addr stores s_ $_, addr addr is one of label ($_) displacement($_)

1. addr is a label Example: lw $8, X memory X //////// $8 //////// 21

2. addr is ($_) the address is in a register Example: lw $10, ($14) memory $14 *** *** //////// $10 //////// 2222

3. addr is displacement($_) an address is in a register Example: lw $12, 4($15) memory $15 1000 + 1004 //////// $12 //////// 2323

Dealing with bytes example: lb $20, X memory 1 byte X  $20     sign extend 2424

More dealing with bytes: lbu $21, Y memory More dealing with bytes: lbu $21, Y 1 byte Y: //////// $21 00000000 00000000 00000000 //////// Zero Extend 2525

sb $13,X Example: XXXXX Only 1 byte of memory changes X memory 2626

la $22, X X X Example: $22 memory Much like int X; int *pX; px = &X; 2727

Some branch instructions label unconditional branch to label beq r1, r2, label branch to label if (r1) == (r2) bne branch to label if (r1) != (r2) bgt branch to label if (r1) > (r2) bge branch to label if (r1) >= (r2) blt branch to label if (r1) < (r2) ble branch to label if (r1) <= (r2)

More branch instructions beqz r1, label branch to label if (r1) == 0 bnez branch to label if (r1) != 0 bgtz branch to label if (r1) > 0 bgez branch to label if (r1) >= 0 bltz branch to label if (r1) < 0 blez branch to label if (r1) <= 0

2 more control instructions j label unconditional jump to label jal unconditional jump to label, with return address in $31

Incorrect, bad, and wrong: Good examples of branch instructions: beq $8, $9, end_program bgtz $16, next_check Incorrect, bad, and wrong: beq X, $12, bad_code Also wrong: ble $10, -1, less_than_case Correct implementation of the ble: li $11, -1 ble $10, $11, less_than_case 31

#C code # # if ( x == 0 ) { # y++; # } 3232

#C code # # if ( x == y ) { # a++; # y = a – 3; # } 3333

# C code # # sum = 0 # for ( i = 0; i < count; i++ ) { # sum = sum + i; # } 3434

# C code # #define MAX 13 # count = 0; # while ( count < MAX ) { # /* code missing! */ # } 3535

# C code # while ( (x >= y) && (z < 300) ) { # z = z – x; # if ( (x % 2) == 0 ) { # x--; # y++; # } # } 3636

I/O instructions putc getc puts print the ASCII character represented by the least significant byte of r1 getc read a character, placing it in the least significant byte of r1 puts r1/label print the null-terminated string that begins at the address within r1 or at the address given by label

# code getc $21 # execution waits for user input of a # character; once the user types an 'R': $21 0x?? 0x?? 0x?? 0x52 3838

# code putc $15 $15 0x52 0x53 0x54 0x55 output U ^ more output goes here, when there is more output 3939

data str1:. asciiz "hello. ". text puts str1 hello .data str1: .asciiz "hello." .text puts str1 hello. ^ more output starts here, when more is printed 4040

.data str1: .asciiz "hello." .text la $12, str1 # address of first # character in string puts $12 # address of first # character to print # is in $12 hello. ^ more output starts here, when more is printed 41

.data str1: .asciiz "hello.\nMy name is George." .text puts str1 hello. My name is George. ^ more output starts here, when more is printed 4242

data str1:. ascii "Hi. \n" str2:. asciiz "I am a badger. " .data str1: .ascii "Hi.\n" str2: .asciiz "I am a badger." .text puts str1 Hi. I am a badger. ^ more output starts here, when more is printed 4343

# MAL program to print out the alphabet. data str1: # MAL program to print out the alphabet .data str1: .asciiz "The alphabet:\n" # register assignments # $8 -- the ASCII character code # to be printed # $9 -- the ASCII code for 'z', the # ending character 4444

__start: la $10, str1 puts $10 add $8, $0, 97 # $8 gets ASCII for 'a'; .text __start: la $10, str1 puts $10 add $8, $0, 97 # $8 gets ASCII for 'a'; # could be li $8, 97 add $9, $0, 122 # $9 gets ASCII for 'z'; # could be li $9, 122 while: bgt $8, $9, all_done putc $8 add $8, $8, 1 b while all_done: li $10, '\n' putc $10 done 4545

# this simple MAL program reads in 2 # characters, figures out which one is # alphabetically first, and prints it out. # register assignments # $8 -- the first character typed by the user # $9 -- the second character typed in # by the user # $10 -- temporary # $11 -- holds the value of the larger # character # $13 -- the address of the newline # character constant # $14 -- newline character (a constant) 4646

.text __start: getc $8 # get 2 characters getc $9 li $14, '\n' # print newline putc $14 # figure out which is larger sub $10, $9, $8 bgez $10, secondlarger add $11, $8, $0 b printresult secondlarger: add $11, $9, $0 printresult: putc $11 done 4747