assembly : adding label to dissasembled code

Discussion in 'Programmer's Corner' started by furtsiv, Feb 24, 2018.

  1. furtsiv

    Thread Starter New Member

    Mar 7, 2017
    16
    0
    hi there community
    i would like to create a 8051 hex from a previous hex file
    i dissembled the hex then i wanted to add some personal code to the begining

    but the original hex have some LJMP and ACALL instructions wich jump to specified adress
    but if i add my personal code then original code addresses must be changed to new values.
    is there an application that can convert for example
    ACALL 0120H
    to
    ACALL label1 (then add this label1 to the address 0120H)
    this way code will jump to specified label instead of wrong address .

    my personal code :
    MOV A,#0
    MOV DPTR,#4000H
    MOVX A,@DPTR
    MOV R0,A
    DJNZ R0,AGAIN
    MOV 0C7H,#0AAH
    MOV 0C7H,#055H
    ORL 087H,#02h

    AGAIN: MOV A,#0
    MOV A,R0
    MOVX @DPTR,A

    and this is the beginning of original hex

    LJMP 002CH
    RETI
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    LJMP 0650H
    RETI
    NOP
    NOP
    NOP
    NOP
    RETI
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    RETI
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    RETI
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    RETI
    LCALL 0819H
    CLR 4CH
    MOV DPTR,#1800H
    MOV A,#0AAH
    MOVX @DPTR,A
    INC DPTR
    MOV A,#0A5H
    MOVX @DPTR,A
    MOV DPTR,#1809H
    MOV A,#0F0H
    MOVX @DPTR,A
    MOV DPTR,#1808H
     
    Last edited: Feb 24, 2018
  2. LesJones

    Well-Known Member

    Jan 8, 2017
    1,497
    356
    It looks like it was not a very good disassembler that was used. Disassemblers that I have used have allways put the labels on the left hand side so you know where the jump or call goes to in the disassembler listing. You may have to do this by hand. I think you will have to look at the memory locations in the HEX file and try to match up a few instructions either side of the location with the disassembler listing. I may be better to try to find a better disassembler program.

    Les.
     
  3. furtsiv

    Thread Starter New Member

    Mar 7, 2017
    16
    0
    thanks for the advise
    i used a better disassembler and now i have labels ,but when i implement my code first then add dissambled code and compile it i get it to work only if my personal code is less than 5 instructions ,if i insert more instructions final hex will not work
    for example this works :

    MOV A,#02H
    MOV A,#03H
    MOV A,#0
    // disassembled code here //

    this will not work:
    MOV A,#01H
    MOV A,#04H
    MOV A,#08H
    MOV A,#02H
    MOV A,#03H
    MOV A,#0
    // disassembled code here //


    the new disassembler code is like this :
    ljmp X002c
    ;
    reti
    ;
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    ;
    ljmp X0650
    ;
    reti
    ;
    NOP
    NOP
    NOP
    NOP
    ;
    reti
    ;
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    ;
    reti
    ;
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    ;
    reti
    ;
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    ;
    reti
    ;
    X002c: lcall X0819
    clr 29h.4
    mov dptr,#X1800
    mov a,#0aah
    movx @dptr,a
    inc dptr
    mov a,#0a5h
    movx @dptr,a
    mov dptr,#X1809
    mov a,#0f0h
    movx @dptr,a
    mov dptr,#X1808
    mov a,#0
    movx @dptr,a
    mov r2,#10h
    X0049: mov dph,#0bbh
    mov a,#9bh
    movx @dptr,a
    mov a,#0
    lcall X0464
    djnz r2,X0049
    mov 3eh,#0ffh
    mov 62h,#80h
    mov 63h,#0
    mov 61h,#8
    mov 64h,#0
    setb 29h.0
    mov 3ah,#50h
    mov 55h,#0ah
    setb 2fh.4
    setb 2fh.5
    setb 2fh.0
    setb 2fh.1
    setb 2fh.2
    setb 2fh.3
    mov 44h,#80h
    mov 45h,#80h
    mov 30h,#80h
    mov 31h,#80h
    mov 40h,#80h
    mov 41h,#80h
    mov 74h,#80h
    mov 75h,#80h
    mov 70h,#80h
    mov 71h,#80h
    mov 72h,#80h
    mov 73h,#80h
    clr 2ah.0
    clr 2ah.1
    clr 2ah.2
    clr 2ah.4
    clr 2ah.5
    clr 2fh.6
    clr 29h.2
    mov 6ah,#50h
    mov 37h,#80h
    mov 38h,#8
    mov 2dh,#0ffh
    mov 2ch,#0ffh
    mov 2bh,#0ffh
    mov a,#0
    lcall X0464
    mov 2dh,#0ffh
    mov 2ch,#0ffh
    mov 2bh,#0ffh
    clr 29h.2
    mov 6ah,#50h
    mov 68h,#0ffh
    mov 67h,#0ffh
    mov 69h,#0ffh
    mov 30h,#0ffh
    mov 70h,#0ffh
    mov 40h,#0ffh
    mov 72h,#0ffh
    mov 44h,#0ffh
    mov 74h,#0ffh
    mov 31h,#0ffh
    mov 71h,#0ffh
    mov 41h,#0ffh
    mov 73h,#0ffh
    mov 45h,#0ffh
    mov 74h,#0ffh
    lcall X0630
    mov a,6eh
    anl a,#7fh
    orl a,#50h
    mov 6eh,a
    mov dptr,#X18ff
    movx @dptr,a
    X010c: lcall X0a0f
    mov dph,#0bbh
    mov a,#9bh
    movx @dptr,a
    lcall X0134
    lcall X04e4
    lcall X0907
    ajmp X010c
    ;
    X0120: mov dptr,#X1910
    movx a,@dptr
    jnz X0133
    mov a,tl0
    anl a,#7fh
    orl a,#50h
    movx @dptr,a
    mov dptr,#X18ff
    movx @dptr,a
    mov 6eh,a
    X0133: ret
    ;
    X0134: jnb 2fh.6,X0142
    djnz 39h,X0142
    mov 39h,#0ah
    jnb 2ah.6,X0142
    cpl p2.3
    X0142: ret
    ;
    X0143: mov dph,#0bah
    movx a,@dptr
    jnb acc.6,X0150
    jnb acc.7,X0150
    ljmp X01b8
    ;
    X0150: mov dph,#0b9h
    movx a,@dptr
    mov c,acc.0
    orl c,acc.1
    jnc X0170
    mov c,acc.2
    orl c,acc.3
    jnc X0170
    mov dph,#0b8h
    movx a,@dptr
    mov c,acc.0
    orl c,acc.1
    jnc X0170
    mov c,acc.2
    orl c,acc.3
    jc X01b8
    X0170: mov dph,#0bah
    movx a,@dptr
    jnb acc.7,X017a
    jb acc.6,X01b8
    X017a: mov 2dh,#0ffh
    mov 2ch,#0ffh
    mov 2bh,#0ffh
    clr 29h.2
    mov 6ah,#10h
    mov 68h,#0ffh
    mov 67h,#0ffh
    mov 69h,#0ffh
    mov 30h,#0ffh
    mov 70h,#0ffh
    mov 40h,#0ffh
    mov 72h,#0ffh
    mov 44h,#0ffh
    mov 74h,#0ffh
    mov 31h,#0ffh
    mov 71h,#0ffh
    mov 41h,#0ffh
    mov 73h,#0ffh
    mov 45h,#0ffh
    mov 74h,#0ffh
    ljmp X0206
    ;
    X01b8: mov dph,#0b8h
    movx a,@dptr
    cjne a,2dh,X01c5
    mov 67h,#80h
    ljmp X01cc
    ;
    X01c5: djnz 67h,X01cc
    mov 2dh,a
    acall X0120
    X01cc: mov dph,#0b9h
    movx a,@dptr
    cjne a,2ch,X01d9
    mov 68h,#80h
    ljmp X01de
    ;
    X01d9: djnz 68h,X01de
    mov 2ch,a
    X01de: mov dph,#0bah
    movx a,@dptr
    cjne a,2bh,X01eb
    mov 69h,#0f0h
    ljmp X01f0
    ;
    X01eb: djnz 69h,X01f0
    mov 2bh,a
    X01f0: anl a,#3fh
    mov b,a
    mov a,2bh
    anl a,#0c0h
    orl a,b
    mov 2bh,a
    mov dptr,#X1802
    mov a,2dh
    movx @dptr,a
    inc dptr
    mov a,2ch
    movx @dptr,a
    X0206: jb 29h.0,X023e
    jb 2bh.3,X0211
    mov a,#10h
    ljmp X023a
    ;
    X0211: jb 2bh.2,X0219
    mov a,#20h
    ljmp X023a
    ;
    X0219: jb 2bh.5,X0221
    mov a,#30h
    ljmp X023a
    ;
    X0221: jb 2bh.0,X0229
    mov a,#40h
    ljmp X023a
    ;
    X0229: jb 2bh.7,X0235
    mov a,#0
    cpl 29h.4
    setb 29h.0
    ljmp X023a
     
  4. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    6,556
    1,526
    I am not familiar with the 8051 however I know that on other processors moving existing code around may fail if the code now crosses a page boundary where it didn't before and other similar effects.

    You could put your new code in some part of memory not currently used and put a call to your code where you want it. The instruction(s) that were originally at the location of the new call location will need to be moved to the end of you new code. You will need to check that there are no references, calls, jumps. etc to the location of the new call instruction.
     
  5. furtsiv

    Thread Starter New Member

    Mar 7, 2017
    16
    0
    my dissasembled code does not have MOVC instruction wich i think is the only one that can be an issue if i move programe around , please anyone correct me if i'm wrong
    calls like ACALL and LJMP goes to labels instead of addresses ,
    any ideas ?
     
  6. LesJones

    Well-Known Member

    Jan 8, 2017
    1,497
    356
    Lables are just sybolic addresses. All you need to do is to put something in front of the addres so it is no longer interpreted as an address by the assembler. For example change X0819 to LocX0819 I have used Loc to stand for location. When you go through the disassembly you will slowly work out what various parts of the code do. For example you may find a subroutine that produced a delay of one second you could change the label to" Del1sec" for example. At first I normaly leave the label contining the address as well as the one that means something to you. Once I have finished I assemble the unmodified code and verify that the addess in the list file from the assembly matches the original address. I normaly start off by identifying instructions that deal with I/O ports. You than know by looking at the schematic what that part of the code is doing.. For example if that I/O port is connected to an LCD display you know it is writing data to the display or getting some status information. (I don't know the 8051 so it could be that I/O ports look just the same as any other address on the memory map.)

    Les
     
  7. furtsiv

    Thread Starter New Member

    Mar 7, 2017
    16
    0
    thanks everyone ,i found it ! without changing code offset at all
    the main hex start with : LJMP 002CH
    so i just changed that address to my personal code address (at the end of memory ) and when my code finish the job it jump to 002CH and everything goes fine
    this is especially done to make the main hex run for limited time of restarts then stop ,like a trial version
     
Loading...