Assembler Programs


Write an assembler program to add the two numbers stored at locations 14h and 15h in RAM and place the answer at location 16h


n3 = n1 + n2;



$mod2051


n1 data 14h

n2 data 15h

n3 data 16h


org 0

mov a,n1

add a,n2

mov n3,a


end



Write an assembler program to subtract the two numbers stored at locations 14h and 15h in RAM and place the answer at location 16h


n3 = n1 - n2;



$mod2051


n1 data 14h

n2 data 15h

n3 data 16h


org 0

mov a,n1

clr c

subb a,n2

mov n3,a


end

Write an assembler program to convert the character stored at location 65h to lower case


c1 |= 0x20;


$mod2051


c1 data 65h


org 0

mov a,c1

orl a,#20h

mov c1,a


end



Write an assembler program to convert a string at location 45h to lower case.


i = 0;

while (s[i]) {

s[i] |= 0x20;

i++;

}


$mod2051

dseg

org 45h

s: ds 32


cseg

org 0

mov r0,#s

mov a,@r0

l1: jz l2

orl a,#20h

mov @r0,a

inc r0

mov a,@r0

jmp l1

l2: nop

end

Write a 2051 assembler program.


When the program starts, R0 points to a string of characters terminated with a 00h byte.


When the program ends, R2 contains the number of consecutive double characters within the string.


Eg


chocolate has no double letters -> R2 = 0

sweet has one -> R2 = 1

address has two -> R2 = 2


i = 0;

doubles = 0;

while (buffer[i] != 0) {

if ((buffer[i]-buffer[i+1])==0) {

doubles++;

}

i++;

}


or (using pointers):


char *r0, *r1;

char a;

int r2;

r2 = 0;

r0 = buffer;

r1 = buffer;

r1++;

a = (*r0)

while ((*r0) != 0) {

a = a – (*r1);

if (a == 0) {

r2++;

}

r0++;

r1++;

a = (*r0);

}

$mod2051

dseg

org 40h

buffer: ds 64

;

cseg

;use R0 to point to first character

;and R1 to point to next one along

;R2 is where we store the answer

;

org 0

;

mov R0,#buffer ;init R0 and R1

mov R1,#buffer

inc R1

mov R2,#00 ;set counter to 0

mov a,@R0 ;look at a char

l0:

jz stop ;stop when its NUL

clr c

subb a,@R1 ;sub next char

jnz l1

inc R2 ;add to counter if zero

l1:

inc R0 ;move to the next pair

inc R1

mov a,@R0 ;look at next char

sjmp l0 ;loop around the string

stop:

sjmp stop

end

159.233 Assembler 7 - 1