I'm taking embedded controller programming at a local college this summer, and the book and the instructor both are awful... I posted this on anohter forum for help and got referred to here (I got no help there, so I figured I'd try the experts!) The controller is a an 8051 derivitive, and one of its oddities is that commands like MOV, ADD, etc... are followed by: destination, source, instead of source, destination... Anyways, My homework is 2 parts... There's some questions in both, and some coding in the end of the second part... If you could look it over for me, it'd be great! If not, thanks for your time anyhow!

(I'm only going to include the code/question part of the first one, it should have everything needed)

; 0) Do the addition below assuming that we are working with
; UNSIGNED numbers. What are the two numbers we are adding (decimal) ?
; 101 and 43
; What is the result ?
; 144
; Is the result valid or not ?
; Yes
; Why or Why not ?
; Both numbers and the correct result are all within the limits of an UNSIGNED 2 digit hex number (0-255)

MOV A, #65h
ADD A, #2Bh

; 1) Do the addition below assuming that we are working with
; UNSIGNED numbers. What are the two numbers we are adding (decimal) ?
; 197 and 75
; What is the result ?
; 16
; Is the result valid or not ?
; No
; Why or Why not ?
; The answer it should be (272) is outside the range of UNSIGNED 2 digit hex numbers (0-255)

MOV A, #0C5h
ADD A, #4Bh

; 2) Now do the addition below assuming that we are working with
; SIGNED numbers. What are the two numbers we are adding (decimal) ?
; -59 and 75
; What is the result ?
; 16
; Is the result valid or not ?
; Yes
; Why or Why not ?
; Both -59 and 75 and the correct result are in the range of a 2 digit SIGNED hex number (-128 to 127)

MOV A, #0C5h
ADD A, #4Bh

; 3) Do the addition below assuming that we are working with
; SIGNED numbers. What are the two numbers we are adding (decimal) ?
; -128 and 127
; What is the result ?
; -1
; Is the result valid or not ?
; Yes
; Why or Why not ?
; Both -128 and 127 and the correct result are in the range of a 2 digit SIGNED hex number (-128 to 127)

MOV A, #80h
ADDC A, #7Fh

; 4) Do the subtraction below assuming that we are working with
; UNSIGNED numbers. What are the two numbers we are adding (decimal) ?
; 147 and 161
; What is the result ?
; 242
; Is the result valid or not ?
; No
; Why or Why not ?
; The answer should be -14, which cannot be represented in an UNSIGNED number

MOV A, #93h
CLR C
SUBB A, #0A1h

; 5) Now do the subtraction below assuming that we are working with
; SIGNED numbers. What are the two numbers we are adding (decimal) ?
; -128 and 1
; What is the result ?
; 127
; Is the result valid or not ?
; No
; Why or Why not ?
; The answer should be -129, which is outside the range of a 2 digit SIGNED hex number (-128 to 127)

MOV A, #80h
CLR C
SUBB A, #01h

; 6) Do the subtraction below assuming that we are working with
; SIGNED numbers. What are the two numbers we are adding (decimal) ?
; -109 and -95
; What is the result ?
; -14
; Is the result valid or not ?
; Yes
; Why or Why not ?
; Because both numbers and the correct result are within the range of a 2 digit SIGNED hex number (-128 to 127)

MOV A, #93h
CLR C
SUBB A, #0A1h





; --------------------------------------------------------------
; EMBEDDED MICROCONTROLLER PROGRAMMING
; HOMEWORK #2 - PROBLEM 2
;
; Written by S.C.T. 1-10-99
; Modified by ALM: April 2, 2001
; --------------------------------------------------------------

; This program demonstrates the use of 16 bit arithmetic and
; subroutine calls on the 8051.

; This is a shell program to demonstrate arithmetic for performing
; 16 bit addition and subtraction on the 8051. The homework assignment
; is to fill in the missing subroutines which perform 16 bit adds on
; the values assigned to the respective program registers.

$MOD52

XH EQU R0 ; Define X High and Low Bytes
XL EQU R1
YH EQU R2 ; Define Y High and Low Bytes
YL EQU R3
ZH EQU R4 ; Define Z High and Low Bytes
ZL EQU R5

ORG 4000h ; Start Program execution at
; Address 4000 hex

MOV XH, #0F0h ; Load X High Byte with F0h
MOV XL, #73h ; Load X Low Byte with 73h

MOV YH, #74h ; Load Y High Byte with 74h
MOV YL, #65h ; Load Y Low Byte with 65h

ACALL ADD16 ; Call ADD16 subroutine

; ---------------- ANSWER QUESTIONS HERE ----------------------
; 1) Assuming the 16 bit values in X and Y are UNSIGNED, what
; are the values in X and Y that we are adding (in decimal)?
; X = 61555 and Y = 29797
; What will the value of Z be on return from the the
; subroutine (in decimal)?
; 25817
; Is this answer valid ? Why ?
; No, because the real answer exceeds the limits of a 16-bit UNSIGNED number (0-65535)
; 2) Repeat 1 assuming X and Y are SIGNED values.
; x = -3981 and Y = 29797
; 25816
; Yes, because both values and the true result are within the boundaries of a 16-bit SIGNED number (-32768 to 32767)
; --------------------------------------------------------------


ACALL SUB16

; ---------------- ANSWER QUESTIONS HERE ----------------------
; 1) Assuming the 16 bit values in X and Y are UNSIGNED, what
; are the values in X and Y that we are subtracting (in decimal)?
; What will the value of Z be on return from the the
; subroutine (in decimal)?
; 31758
; Is this answer valid ?
; Yes
; Why ?
; Because both values and the result are in the boundaries of a 16-bit SIGNED number (0-65535)
;
; 2) Repeat 1 assuming X and Y are SIGNED values.
; 31758
; No
; Because the answer it should be (-33778) is not within the boundaries of a 16-bit SIGNED number (-32768 to 32767)
; --------------------------------------------------------------


; ------------ FILL IN SUBROUTINES ADD16 and SUB16 Below -------
; 3) These two subroutines should perform 16 bit additions and
; subtractions (respectively) on the 16 bit values stored in
; X and Y and store the results in Z. The high and low bytes
; in X,Y, and Z are denoted as XH,XL, YH,YL, and ZH,ZL.

ADD16:

MOV A, XL
ADD A, YL
MOV ZL, A
MOV A, XH
ADDC A, YH
MOV ZH, A

RET



SUB16:

MOV 0D0h, #00h
MOV A, XL
SUBB A, YL
MOV ZL, A
MOV A, XH
SUBB A, YH
MOV A, ZH

RET


END
Posted on 2001-07-24 16:45:02 by The Lost
So you want us to make your homework? looks somehow allready done...then what?

MOV destination,source ....

Is the standard notation for Intel(c) so i see no odd here ;)
Posted on 2001-07-24 17:59:51 by BogdanOntanu
Nonononono, I don't want you to do it, its ALREADY DONE... I wanted you guys to CHECK it... Really only the last part:

ADD16:

MOV A, XL
ADD A, YL
MOV ZL, A
MOV A, XH
ADDC A, YH
MOV ZH, A

RET



SUB16:

MOV 0D0h, #00h
MOV A, XL
SUBB A, YL
MOV ZL, A
MOV A, XH
SUBB A, YH
MOV A, ZH

RET
Posted on 2001-07-24 18:14:15 by The Lost
I think you forgot something, what if XL+YL don't fit in A (if I understood A is 8 bits) ?

(s)
Posted on 2001-07-25 10:33:45 by (scalp)
There's a tutorial here.
Posted on 2001-07-25 10:42:30 by Dr. Manhattan
this reminds me on old z80 days. And which statement does clear carry flag in SUB16?

SUB16:

MOV 0D0h, #00h ;dont know
CLR C ; isnt this statement missing?
MOV A, XL
SUBB A, YL
MOV ZL, A
MOV A, XH
SUBB A, YH
MOV A, ZH ;i think this should be MOV ZH,A instead

RET
Posted on 2001-07-25 11:16:05 by Unregistered
This thread really belongs in The Heap.

To (scalp): The carry is handled by ADDC.

To The Lost:

Well, I thought you forgot to initialize the carry for the subtract, but then I had to go check the '51 manual to verify the SFR you are using is correct.

Although MOV 0D0h,#00h clears the C flag, it is bad practice for several reasons.

1) Obscurity. '51 assemblers will either predefine or allow the definition of the name "PSW" for this register.

2) Correctness. It affects more than just the C flag. There are three bits you don't want to change unless the change is really needed.

3) Size. The more obvious CLR C instruction is one byte long, as opposed to three bytes for MOV 0D0h,#00h.

4) Speed. CLR C takes one machine cycle, MOV 0D0h,#00h takes two or three cycles (depending on '51 variant).
Posted on 2001-07-25 12:04:59 by tank
thanx Tank but I knew that but I didn't want to resolve it

(s)
Posted on 2001-07-26 13:51:40 by (scalp)