简体   繁体   中英

Correct Template for an Assembly Program

I have just started off with assembly and was looking at some sample programs shared by the instructor, for example, the below program to swap 2 numbers.

ORG 0h
; This line tells the MCR to place the first instruction at add 0
; But does this have to be the first statement? I tried writing the line
; 'num1 EQU #20h' before org, but this was throwing an error

LJMP main
; This line transfers the control to the main block unconditionally
ORG 100h
; Why do we need this? The code if for the 8051 which has a total RAM range 
; of 256B, so this address seems out of range 
main:
    MOV 70H,#20H
    MOV 71H,#21H

    MOV A,70H
    MOV 70H,71H
    MOV 71H,A
    ; The accumulator A acts like a temp in a simple C program

    HERE:SJMP HERE
    ; What is the purpose of this line?
END

Kindly help in resolving my questions (as code comments) about this template


Edit

The issue with label_name EQU const_value seems to be something else, I am getting a syntax error no matter where I place the line

first instruction at add 0 ; But does this have to be the first statement?

Probably not, but it makes the file simpler to understand. Real world code would probably start with some include statements to load macros. But that is beyond scope in a beginner lesson.

The code if for the 8051 which has a total RAM range ; of 256B, so this address seems out of range

RAM != ROM The 8051 is a havard machine and executes instructions only from program memory, which is read-only (mostly read only in case of flash variants). It cannot execute code from RAM. Most 8051 have a few KB ROM or more.

You need the jump to address 100h because of the interrupt table. You will get to this in a future lesson.

HERE:SJMP HERE ; What is the purpose of this line?

There is no magic "stop" instruction, but a jump to the address of the current instruction has a very similar effect of halting the program flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM