Assembly Language

12
Assembly Language part 2

description

Assembly Language. part 2. Program example 2. BR 0x0008 .ASCII "#?" .ASCII "\n" .BLOCK 2 CHARO 0x0003, d CHARO 0x0004, d DECI 0x0006, d CHARO 0x0005, d DECO 0x0006, d STOP .END. go past data – minimizes offset calculations (contrast with data after code). “constant” declarations. - PowerPoint PPT Presentation

Transcript of Assembly Language

Page 1: Assembly Language

Assembly Language

part 2

Page 2: Assembly Language

Program example 2BR 0x0008.ASCII "#?".ASCII "\n".BLOCK 2CHARO 0x0003, dCHARO 0x0004, dDECI 0x0006, dCHARO 0x0005, dDECO 0x0006, dSTOP.END

go past data – minimizes offsetcalculations (contrast with data after code)

“constant” declarations

“variable” declaration

output prompt

read number

output newline character

output number

Page 3: Assembly Language

Data declaration & storage

• The previous example included two different types of data declaration instructions:– the .ASCII pseudo-op is used to allocate a

contiguous set of bytes large enough to hold the specified data; as with Java & C++, the backslash character (\) is used as the escape character for control codes, like newline

– the .BLOCK pseudo-op allocates the specified number of bytes and initializes their values to 0

Page 4: Assembly Language

Data declaration & storage

• Two other pseudo-ops provide data storage:– the .WORD instruction allocates two bytes,

suitable for storage of integers– the .BYTE instruction allocates one byte,

suitable for storage of characters– like .ASCII (and unlike .BLOCK), both of these

instructions allow the programmer to specify initial values, as shown on next slide

Page 5: Assembly Language

Initialization examples

• .WORD 7 ; allocates 2 bytes, with

; decimal value 7

• .BYTE 0x2B ; allocate 1 byte, with hex

; value 2B (‘+’)

Page 6: Assembly Language

I/O instructions

• The DECI and DECO instructions considerably ease the process of reading and writing numbers

• Each one deals with word-size data, and represent instructions not available in the underlying machine language – thus they are part of the set of unimplemented op codes

• The actual I/O is performed by the operating system; the instructions generate program interrupts that allow the OS to temporarily take over to provide a service to the program

Page 7: Assembly Language

I/O instructions

• The CHARI and CHARO instructions are simply assembly language versions of the machine language input and output instructions:– read or write a byte of data– data source (for output) and destination (for

input) are memory (not registers)

Page 8: Assembly Language

I/O instructions

• STRO is yet another example of an unimplemented op code

• Outputs a string of data– String can be predefined with the .ASCII

pseudo-op– Predefined string must be terminated with a

null character: “\x00”

Page 9: Assembly Language

Arranging instructions and data

• In the first program example (see Monday’s notes), as with all of the machine language examples, instructions were placed first, ended with a STOP code, and data followed

• Problems with this approach:– requires address calculations based on the number of

instructions (which may not be known as you’re writing a particular instruction)

– addresses may have to be adjusted if even minor changes are made to the program

Page 10: Assembly Language

Putting the data first

• An easy solution to the problems described on the previous slide was illustrated by the program example; the solution is twofold:– declare the data first– place an unconditional branch instruction at

the beginning of the program, pointing to the first instruction after the data

– the following example provides another illustration

Page 11: Assembly Language

Program example 3br 0x0020 ; bypass data.block 4 ; space for 2 ints.ascii "Enter a number: \x00".ascii " + \x00".ascii " = \x00"stro 0x0007,d ; promptdeci 0x0003,d ; get 1st numberstro 0x0007,d ; promptdeci 0x0005,d ; get 2nd numberdeco 0x0003,d ; output 1st numberstro 0x0018,d ; output ascii string " + "deco 0x0005,d ; output 2nd numberstro 0x001c,d ; output string " = "lda 0x0003,d ; put the first # in Aadda 0x0005,d ; add 2nd # to firststa 0x0003,d ; store sumdeco 0x0003,d ; output sumstop.end

Page 12: Assembly Language

Program example 4: using labels

br codepirate: .ASCII "Arrr!\x00"code: stro pirate ,d STOP.END