Skip to main content

Get data from P0, send to P1 (asm and C)

Short answer

Configure Port 0 as input by writing all ones; then continuously read its value and write to Port 1. Assembly: MOV P0,#0FFH; MOV A,P0; MOV P1,A; SJMP. Equivalent C: P0=0xFF; while(1) P1=P0;. External pull-up resistors are needed on Port 0.

Write an assembly program to get data from P0 and send it to P1 and compare with corresponding C program.

Port 0 is made an input by writing 1s to it; then its value is repeatedly read into A and written to Port 1.

Assembly

        ORG 0000H
        MOV P0,#0FFH      ; configure Port 0 as input
BACK:   MOV A,P0          ; read the data at P0 into A
        MOV P1,A          ; send it to P1
        SJMP BACK         ; repeat forever
        END

Equivalent C

#include <reg51.h>
void main(void)
{
    P0 = 0xFF;            /* Port 0 as input */
    while (1)
        P1 = P0;          /* read P0, write to P1 */
}

Comparison

PointAssemblyC
Length4 instructions1 statement in the loop
ControlFull control of registers and timingCompiler chooses registers
ReadabilityLowHigh
PortabilityOnly 8051Easily ported to other controllers
Speed, code sizeSmallest and fastestSlightly larger, but acceptable
Development timeLongerShorter

Both programs behave the same. Port 0 has no internal pull-ups (its pins are open-drain), so external pull-up resistors (about 10 kΩ) are normally added on P0 when it reads switches or open-drain outputs.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Embedded System chapter 9 questions →