120 likes | 278 Views
Assembly Language. part 2. Program example 2. BR 0x0008 .ASCII "#?" .ASCII "<br>" .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.
E N D
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 “variable” declaration output prompt read number output newline character output number
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
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
Initialization examples • .WORD 7 ; allocates 2 bytes, with ; decimal value 7 • .BYTE 0x2B ; allocate 1 byte, with hex ; value 2B (‘+’)
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
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)
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”
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
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
Program example 3 br 0x0020 ; bypass data .block 4 ; space for 2 ints .ascii "Enter a number: \x00" .ascii " + \x00" .ascii " = \x00" stro 0x0007,d ; prompt deci 0x0003,d ; get 1st number stro 0x0007,d ; prompt deci 0x0005,d ; get 2nd number deco 0x0003,d ; output 1st number stro 0x0018,d ; output ascii string " + " deco 0x0005,d ; output 2nd number stro 0x001c,d ; output string " = " lda 0x0003,d ; put the first # in A adda 0x0005,d ; add 2nd # to first sta 0x0003,d ; store sum deco 0x0003,d ; output sum stop .end
Program example 4: using labels br code pirate: .ASCII "Arrr!\x00" code: stro pirate ,d STOP .END