Assembly

String Output

As we have seen before, our program has a code part and a data part. Suppose we want to output the string "Hello, World" on the screen. We must allocate space in the data part for the message (i.e. the string) that we wish to print.

Consider the following program:

; Description: Program to print out the string 'Hello, World'

; Registers used: ax, dx

; Segment register used: ds

;

; Authors: RC/RJ Class: Cnoc1 Date: Mar. 1998

title prog4.asm

.modelsmall

.stack100h

.data

Helomsgdb 'Hello, World',13,10,'$'

.code

movax, @data; address of data segment to ax

movds, ax; sets ds to point to the data segment,

; need only do this once

movah, 9; DOS function call 9, string output

movdx, offset Helomsg; point to string Helomsg

int21h; get OS to display string

movah, 4ch; terminate the program

int21h

end

Here we have declared some data in the data segment, in particular the string named Helomsg. This string is 14 characters long, it is terminated by the '$'. It will have a particular offset within the data segment. So memory will look like:

Figure 8.1 A string in memory

Points to note:

1.The address of the start of the data segment is put into ax, this is then copied into ds (the data segment register), in the lines:

movax, @data; address of data segment to ax

movds, ax; sets ds to point to the data segment,

; need only do this once

It is not permissable to move the address of data directly into a segment register.

So the lines above could not be replaced by the single line of code:

mov ds , @data

as this would give an error when assembled. The address of data must be placed in a general register first.

2.The offset of this message is put into dx, to indicate where the start of the string is stored in the data segment of memory (see diagram 8.1 above). The offset is the number of bytes in memory past the start of the data segment.

3.The function call number for printing a string is 9, this is placed into ah and the software interrupt, int 21h , for I/O is called.

4.The operating system then prints the string on screen, character by character, starting from the address pointed to by dx, until the '$' symbol is reached.

Another Sample Program:

; The following program reads a character from the user, and if

; the character is lowercase, it outputs it in uppercase

; RJ/RC MAR ‘98

title prog5.asm

.model small

lowerAequ97 ; define a constant, no memory allocated

lowerZequ122; define a constant

spaceequ32; define a constant

CRequ13; define a constant

LFequ10; define a constant

.stack 100h

.data

entchardb'Enter character: ','$'

.code

movax, @data

movds, ax; sets ds to point to the data segment,

; need only do this once

movah, 9; DOS function call 9, string output

movdx, offset entchar; point to string entchar

int 21h ; output the string

movah, 1; input a character, return in al

int21h

movbl , al; move the character input to bl

cmpbl, lowerA; compare bl to ‘lowerA’

jbfinish; if bl < ‘a’ , jump to ‘finish’

cmpbl, lowerZ; compare bl to ‘lowerZ

jafinish; if bl > ‘z’, jump to finish

subbl, space; convert lowercase to uppercase

finish:movdl, CR

movah, 2

int21h; outputs a carriage return

movdl, LF

int21h; outputs a line feed

movdl, bl; put character to be outputted in dl

int21h; output character

mov ah, 4ch

int 21h; terminate program

end

Points to note:

1.The statement equ is used here to simply define constants, these are then stored in the assembler’s symbol table. No memory is allocated for these constants.

2.The statement cmp, is used to compare the value in the 1st operand to that in the 2nd operand, (imagine it as subtracting the second from the first). This result can be used with the following jump operations (for use with unsigned numbers) :

jbjump if below

jbe<=jump if below or equal

je=jump if equal

jajump if above

jae>=jump if above or equal

jne!=jump if not equal

3.The next lecture will look in more detail at the concept of jump conditions in assembly

4.The sub operation simply subtracts the second operand from the first and stores the result in the first operand, i.e.

sub bl, space

will subtract 32 from the value in register bl and store the result in bl.

If you check the ascii table you will notice that the difference between ‘a’ and ‘A’ is 32 and similarly for all other letters on the ascii table.

10/19/2018Lect0898.doc - String OutputPage: 1