19
Organização Básica de computadores e linguagem de montagem 1 o Semestre de 2011 Prof. Edson Borin

Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Organização Básica de computadores e linguagem de

montagem

1o Semestre de 2011

Prof. Edson Borin

Page 2: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Sintaxe e Diretivas do Montador

• AVR Studio 4 (Website da Atmel ou do curso de mc404)

• Guia do usuário para linguagem de montagem do AVR (manual assemblerdoc1022.pdf)

Page 3: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

O Arquivo Fonte

• Arquivo fonte é um arquivo texto com o programa em linguagem de montagem. Este arquivo possui:–Mnemônicos de instruções (add, ld, ..)–Labels (Loop:, main:, ...)–Diretivas de montagem (BYTE, CSEG, DEF, ...)–Comentários descrevendo o programa ( ; Comentários

após ponto e vírgula)–Linhas em branco

• O arquivo fonte não possui diretivas de alto nível (if, for, while, etc...)

Page 4: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Sintaxe do arquivo– [Label:] diretiva [operandos] [comentários]– [Label:] instrução [operandos] [comentários]– comentários– Linhas em branco

• Todo texto após “;” é comentário

• Exemplo (Assembler User Guide - Pg 4-4):

label:! .EQU var1=100! ; associa a constante 100 à var1 (Diretiva)! ! ! ! .EQU var2=200! ; associa a constante 200 à var2 (Diretiva)

test:!! rjmp test! ! ! ; laço infinito (Instrução)! ! ! ! ! ! ! ! ! ! ! ; linha com comentário

! ! ! ! ! ! ! ! ! ! ! ! ; outro comentário

O Arquivo Fonte

Page 5: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

SRAM/DSEG

Progr ou EEPROM/CSEG ou ESEG

Progr ou EEPROM/CSEG ou EEPROM

Label constante (não alterável)

Label constante (alterável)

AVR Assembler User Guide

Development Tools User Guide 4-9

The Assembler is not case sensitive. The operands have the following forms:

Rd: R0-R31 or R16-R31 (depending on instruction)Rr: R0-R31 b: Constant (0-7), can be a constant expressions: Constant (0-7), can be a constant expressionP: Constant (0-31/63), can be a constant expressionK: Constant (0-255), can be a constant expressionk: Constant, value range depending on instruction.

Can be a constant expression.q: Constant (0-63), can be a constant expression

4.5 Assembler directives

The Assembler supports a number of directives. The directives are not translateddirectly into opcodes. Instead, they are used to adjust the location of the program inmemory, define macros, initialize memory and so on. An overview of the directives isgiven in the following table. Summary of directives:

Note: All directives must be preceded by a period.

Directive Description

BYTE Reserve byte to a variableCSEG Code SegmentDB Define constant byte(s) DEF Define a symbolic name on a registerDEVICE Define which device to assemble forDSEG Data SegmentDW Define constant word(s)ENDMACRO End macroEQU Set a symbol equal to an expressionESEG EEPROM SegmentEXIT Exit from fileINCLUDE Read source from another fileLIST Turn listfile generation onLISTMAC Turn macro expansion onMACRO Begin macroNOLIST Turn listfile generation offORG Set program originSET Set a symbol to an expression

Diretivas do Montador

Page 6: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Diretivas de Segmentos–.CSEG ; Segmento de Memória na Flash–.DSEG ; Segmento de Memória na SRAM–.ESEG ; Segmento de Memória na EEPROM

• Exemplo: (Assembler User Guide - Pg 4-10)

Diretivas do MontadorAVR Assembler User Guide

4-10 Development Tools User Guide

4.5.1 BYTE - Reserve bytes to a variable

The BYTE directive reserves memory resources in the SRAM. In order to be able torefer to the reserved location, the BYTE directive should be preceded by a label. Thedirective takes one parameter, which is the number of bytes to reserve. The directivecan only be used within a Data Segment (see directives CSEG, DSEG and ESEG).Note that a parameter must be given. The allocated bytes are not initialized. Syntax:

LABEL: .BYTE expression

Example:.DSEG

var1: .BYTE 1 ; reserve 1 byte to var1

table: .BYTE tab_size ; reserve tab_size bytes

.CSEG

ldi r30,low(var1) ; Load Z register low

ldi r31,high(var1) ; Load Z register high

ld r1,Z ; Load VAR1 into register 1

4.5.2 CSEG - Code Segment

The CSEG directive defines the start of a Code Segment. An Assembler file can consistof several Code Segments, which are concatenated into one Code Segment whenassembled. The BYTE directive can not be used within a Code Segment. The defaultsegment type is Code. The Code Segments have their own location counter which is aword counter. The ORG directive (see description later in this document) can be used toplace code and constants at specific locations in the Program memory. The directivedoes not take any parameters.Syntax:

.CSEG

Example:.DSEG ; Start data segment

vartab: .BYTE 4 ; Reserve 4 bytes in SRAM

.CSEG ; Start code segment

const: .DW 2 ; Write 0x0002 in prog.mem.

mov r1,r0 ; Do something

Page 7: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Reserva de bytes e reserva de bytes com inicialização–Reservando bytes para variáveis.DSEG:! ! ! ! var1:!.BYTE 1!! ; reserve 1 byte para var1! ! ! ! table: .BYTE 16! ; reserve 16 bytes.CSEG:

!! ldi r30, low(var1)! ; inicialize a parte baixa de Z!! ldi r31, high(var1)!; inicialize a parte alta de Z!! ld r1, Z!! ! ! ; carregue VAR1 no registrador r1

–Reservando bytes com inicialização.CSEG:! ! ! ! consts:!.DB 0, 255, 0b01010101, -128, 0xaa.ESEG:

!! ! eeconst:!.DB 0xff

Diretivas do Montador

Page 8: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Reserva de palavras com inicialização• Sintaxe:–[label] .DW palavra_1[, palavra_2, ..., palavra_N]

• Exemplo (Assembler User Guide - Pg 4-13):.CSEG:! ! ! ! varlist:!! .DW 0, 0xfffff, -32768, 65535.ESEG:

!! ! eevar:!! ! .DW 0xffff

Diretivas do Montador

Page 9: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Nomes simbólicos para registradores• Sintaxe: –.DEF nome=reg

• Exemplo (Assembler User Guide - Pg 4-11):.DEF temp=r16.DEF indice=r0.CSEG:

!! ldi temp, 0xf0! ; inicialize o reg. temp com 0xf0!! in ior, 0x3f!! ; carregue SREG no reg. ior!! eor temp, ior!! ; ou exclusivo de temp e ior

Diretivas do Montador

Page 10: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Representando expressões com símbolos• Sintaxe: –.EQU label = expressão

• Exemplo (Assembler User Guide - Pg 4-13):.EQU io_offset=0x23.EQU porta=io_offset + 2.CSEG:!! ! ! ! ! ! ! ; Start code segment

!! clr!r2! ! ! ! ; Clear register 2!! out porta, r2!; Write to Port A

Diretivas do Montador

Page 11: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Representando expressões com símbolos usando .SET–.SET permite que o símbolo (label) seja redefinido

mais à frente no programa.• Sintaxe: –.SET label = expressão

• Exemplo (Assembler User Guide - Pg 4-13):.SET offset=0x23.SET porta=offset + 2.SET offset=0x11.CSEG:!! ! ! ! ! ! ! ; Start code segment

!! clr!r2! ! ! ! ; Clear register 2!! out porta, r2!; Write to Port A

Diretivas do Montador

Page 12: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Especificando o endereço a ser usado pelo montador• Sintaxe:–.ORG expressão

• Exemplo (Assembler User Guide - Pg 4-16):.DSEG !! ! ! ! ! ! ! ; escrever no segmento de dados.ORG 0x67!! ! ! ! ! ! ; especificar a localização na SRAM! ! var1:!! .BYTE ; reservar um byte na SRAM (end. 0x67)! ! var2:!! .BYTE ; reservar um byte na SRAM (end. 0x68).ESEG !! ! ! ! ! ! ! ; escrever no segmento da EEPROM.ORG 0x20!! ! ! ! ! ! ; especificar a localização na EEPROM! ! eevar:! .DW 0xfeff ; inicialize uma palavra (end. 0x20).CSEG ; escrever no segmento da Flash.ORG 0x10 !! ! ! ! ; especificar a localização na Flash! ! mov r0, r1! ! ! ! ; faça algo (end. 0x10)

Diretivas do Montador

Page 13: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Expressões–Todas expressões são internamente de 32 bits–São compostas de operandos, operadores e funções

• Operandos–Símbolos (labels) definidos pelo usuário (SET, EQU)–Números inteiros constantes•Decimal: 10, 255•Hexadecimal: 0x0a, $0a, 0xff, $ff•Binário: 0b00110101

–PC: localização atual do programa na memória

Diretivas do Montador

Page 14: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

• Funções–LOW(expression) returns the low byte of an expression–HIGH(expression) returns the second byte of an expression–BYTE2(expression) is the same function as HIGH–BYTE3(expression) returns the third byte of an expression–BYTE4(expression) returns the fourth byte of an expression–LWRD(expression) returns bits 0-15 of an expression–HWRD(expression) returns bits 16-31 of an expression–PAGE(expression) returns bits 16-21 of an expression–EXP2(expression) returns 2^expression–LOG2(expression) returns the integer part of log2(expression)

Diretivas do Montador

Page 15: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Diretivas do Montador

• OperadoresAVR Assembler User Guide

4-18 Development Tools User Guide

4.6.3.1 Logical Not Symbol: !

Description: Unary operator which returns 1 if the expression was zero, and returns 0if the expression was nonzero

Precedence: 14Example: ldi r16,!0xf0 ; Load r16 with 0x00

4.6.3.2 Bitwise Not Symbol: ~Description: Unary operator which returns the input expression with all bits invertedPrecedence: 14Example: ldi r16,~0xf0 ; Load r16 with 0x0f

4.6.3.3 Unary Minus Symbol: -Description: Unary operator which returns the arithmetic negation of an expressionPrecedence: 14Example: ldi r16,-2 ; Load -2(0xfe) in r16

4.6.3.4 Multiplication Symbol: *Description: Binary operator which returns the product of two expressionsPrecedence: 13Example: ldi r30,label*2 ; Load r30 with label*2

4.6.3.5 Division Symbol: /Description: Binary operator which returns the integer quotient of the left expression

divided by the right expressionPrecedence: 13Example: ldi r30,label/2 ; Load r30 with label/2

4.6.3.6 Addition Symbol: +Description: Binary operator which returns the sum of two expressionsPrecedence: 12Example: ldi r30,c1+c2 ; Load r30 with c1+c2

4.6.3.7 Subtraction Symbol: -Description: Binary operator which returns the left expression minus the right

expressionPrecedence: 12

Example: ldi r17,c1-c2 ;Load r17 with c1-c2

4.6.3.8 Shift left Symbol: <<Description: Binary operator which returns the left expression shifted left a number of

times given by the right expression

Precedence: 11Example: ldi r17,1<<bitmask ;Load r17 with 1 shifted

;left bitmask times

Page 16: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Diretivas do Montador

• Operadores

AVR Assembler User Guide

4-18 Development Tools User Guide

4.6.3.1 Logical Not Symbol: !

Description: Unary operator which returns 1 if the expression was zero, and returns 0if the expression was nonzero

Precedence: 14Example: ldi r16,!0xf0 ; Load r16 with 0x00

4.6.3.2 Bitwise Not Symbol: ~Description: Unary operator which returns the input expression with all bits invertedPrecedence: 14Example: ldi r16,~0xf0 ; Load r16 with 0x0f

4.6.3.3 Unary Minus Symbol: -Description: Unary operator which returns the arithmetic negation of an expressionPrecedence: 14Example: ldi r16,-2 ; Load -2(0xfe) in r16

4.6.3.4 Multiplication Symbol: *Description: Binary operator which returns the product of two expressionsPrecedence: 13Example: ldi r30,label*2 ; Load r30 with label*2

4.6.3.5 Division Symbol: /Description: Binary operator which returns the integer quotient of the left expression

divided by the right expressionPrecedence: 13Example: ldi r30,label/2 ; Load r30 with label/2

4.6.3.6 Addition Symbol: +Description: Binary operator which returns the sum of two expressionsPrecedence: 12Example: ldi r30,c1+c2 ; Load r30 with c1+c2

4.6.3.7 Subtraction Symbol: -Description: Binary operator which returns the left expression minus the right

expressionPrecedence: 12

Example: ldi r17,c1-c2 ;Load r17 with c1-c2

4.6.3.8 Shift left Symbol: <<Description: Binary operator which returns the left expression shifted left a number of

times given by the right expression

Precedence: 11Example: ldi r17,1<<bitmask ;Load r17 with 1 shifted

;left bitmask times

Page 17: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Diretivas do Montador

• Operadores AVR Assembler User Guide

Development Tools User Guide 4-19

4.6.3.9 Shift right Symbol: >>

Description: Binary operator which returns the left expression shifted right a number of times given by the right expression.

Precedence: 11Example: ldi r17,c1>>c2 ;Load r17 with c1 shifted

;right c2 times

4.6.3.10 Less than Symbol: <Description: Binary operator which returns 1 if the signed expression to the left is Less

than the signed expression to the right, 0 otherwise Precedence: 10Example: ori r18,bitmask*(c1<c2)+1 ;Or r18 with

;an expression

4.6.3.11 Less or Equal Symbol: <=Description: Binary operator which returns 1 if the signed expression to the left is Less

than or Equal to the signed expression to the right, 0 otherwisePrecedence: 10Example: ori r18,bitmask*(c1<=c2)+1 ;Or r18 with

;an expression

4.6.3.12 Greater than Symbol: >Description: Binary operator which returns 1 if the signed expression to the left is

Greater than the signed expression to the right, 0 otherwisePrecedence: 10Example: ori r18,bitmask*(c1>c2)+1 ;Or r18 with

;an expression

4.6.3.13 Greater or Equal Symbol: >=Description: Binary operator which returns 1 if the signed expression to the left is

Greater than or Equal to the signed expression to the right, 0 otherwisePrecedence: 10

Example: ori r18,bitmask*(c1>=c2)+1 ;Or r18 with;an expression

4.6.3.14 Equal Symbol: ==Description: Binary operator which returns 1 if the signed expression to the left is

Equal to the signed expression to the right, 0 otherwisePrecedence: 9Example: andi r19,bitmask*(c1==c2)+1 ;And r19 with

;an expression

Page 18: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Diretivas do Montador

• Operadores

AVR Assembler User Guide

Development Tools User Guide 4-19

4.6.3.9 Shift right Symbol: >>

Description: Binary operator which returns the left expression shifted right a number of times given by the right expression.

Precedence: 11Example: ldi r17,c1>>c2 ;Load r17 with c1 shifted

;right c2 times

4.6.3.10 Less than Symbol: <Description: Binary operator which returns 1 if the signed expression to the left is Less

than the signed expression to the right, 0 otherwise Precedence: 10Example: ori r18,bitmask*(c1<c2)+1 ;Or r18 with

;an expression

4.6.3.11 Less or Equal Symbol: <=Description: Binary operator which returns 1 if the signed expression to the left is Less

than or Equal to the signed expression to the right, 0 otherwisePrecedence: 10Example: ori r18,bitmask*(c1<=c2)+1 ;Or r18 with

;an expression

4.6.3.12 Greater than Symbol: >Description: Binary operator which returns 1 if the signed expression to the left is

Greater than the signed expression to the right, 0 otherwisePrecedence: 10Example: ori r18,bitmask*(c1>c2)+1 ;Or r18 with

;an expression

4.6.3.13 Greater or Equal Symbol: >=Description: Binary operator which returns 1 if the signed expression to the left is

Greater than or Equal to the signed expression to the right, 0 otherwisePrecedence: 10

Example: ori r18,bitmask*(c1>=c2)+1 ;Or r18 with;an expression

4.6.3.14 Equal Symbol: ==Description: Binary operator which returns 1 if the signed expression to the left is

Equal to the signed expression to the right, 0 otherwisePrecedence: 9Example: andi r19,bitmask*(c1==c2)+1 ;And r19 with

;an expressionAVR Assembler User Guide

4-20 Development Tools User Guide

4.6.3.15 Not Equal Symbol: !=Description: Binary operator which returns 1 if the signed expression to the left is Not

Equal to the signed expression to the right, 0 otherwisePrecedence: 9Example: .SET flag=(c1!=c2) ;Set flag to 1 or 0

4.6.3.16 Bitwise And Symbol: &Description: Binary operator which returns the bitwise And between two expressionsPrecedence: 8Example: ldi r18,High(c1&c2) ;Load r18 with an expression

4.6.3.17 Bitwise Xor Symbol: ^Description: Binary operator which returns the bitwise Exclusive Or between two

expressionsPrecedence: 7Example: ldi r18,Low(c1^c2) ;Load r18 with an expression

4.6.3.18 Bitwise Or Symbol: |Description: Binary operator which returns the bitwise Or between two expressionsPrecedence: 6Example: ldi r18,Low(c1|c2) ;Load r18 with an expression

4.6.3.19 Logical And Symbol: &&Description: Binary operator which returns 1 if the expressions are both nonzero, 0

otherwisePrecedence: 5Example: ldi r18,Low(c1&&c2) ;Load r18 with an expression

4.6.3.20 Logical Or Symbol: ||Description: Binary operator which returns 1 if one or both of the expressions are

nonzero, 0 otherwisePrecedence: 4Example: ldi r18,Low(c1||c2) ;Load r18 with an expression

Page 19: Organização Básica de computadores e linguagem de montagemedson/disciplinas/mc404/2011-1s/slides/… · • Exemplo: (Assembler User Guide - Pg 4-10) Diretivas do Montador AVR

Diretivas do Montador

• Operadores

AVR Assembler User Guide

4-20 Development Tools User Guide

4.6.3.15 Not Equal Symbol: !=Description: Binary operator which returns 1 if the signed expression to the left is Not

Equal to the signed expression to the right, 0 otherwisePrecedence: 9Example: .SET flag=(c1!=c2) ;Set flag to 1 or 0

4.6.3.16 Bitwise And Symbol: &Description: Binary operator which returns the bitwise And between two expressionsPrecedence: 8Example: ldi r18,High(c1&c2) ;Load r18 with an expression

4.6.3.17 Bitwise Xor Symbol: ^Description: Binary operator which returns the bitwise Exclusive Or between two

expressionsPrecedence: 7Example: ldi r18,Low(c1^c2) ;Load r18 with an expression

4.6.3.18 Bitwise Or Symbol: |Description: Binary operator which returns the bitwise Or between two expressionsPrecedence: 6Example: ldi r18,Low(c1|c2) ;Load r18 with an expression

4.6.3.19 Logical And Symbol: &&Description: Binary operator which returns 1 if the expressions are both nonzero, 0

otherwisePrecedence: 5Example: ldi r18,Low(c1&&c2) ;Load r18 with an expression

4.6.3.20 Logical Or Symbol: ||Description: Binary operator which returns 1 if one or both of the expressions are

nonzero, 0 otherwisePrecedence: 4Example: ldi r18,Low(c1||c2) ;Load r18 with an expression