0
$\begingroup$

I'm trying to convert a x86 assembly language program to binary. I have read a 1979 Intel 8086 manual and so understand the basic architecture, however I am still unsure as to how to convert this. Here's the code:

MOV BX, 1300H                          ; Load the Base Address 1st input Matrix in BX
MOV BP, 1400H                          ; Load the Base Address 2nd input Matrix in BP
MOV SI,0001H                           ; Initialize pointer for element of matrix
MOV DI,1501H                           ; Set DI register as pointer for sum matrix
MOV CL,09H                             ; Set CL as count for element in Matrix
REPEAT:
MOV AL,[BX+SI]                         ; Get the element of 1st Matrix in AL
ADD AL,[BP+SI]                         ; Add corresponding element of 2nd Matrix to AL
MOV [DI],AL                            ; Store the Sum of element in Memory
INC SI                                 ; Increment the Pointers
INC DI                                 ;
LOOP REPEAT                            ; Repeat the Addition until count is zero
HLT     
$\endgroup$
5
  • $\begingroup$ Out of curiosity, why do you want to do so? That is basically the assember's duty. $\endgroup$
    – Chaos
    Commented Oct 2, 2023 at 8:25
  • $\begingroup$ Well I'm a computer science undergraduate and I want to do so in order to gain a better understanding of binary and processor architecture. Curiosity plays a part too $\endgroup$
    – Hannah
    Commented Oct 2, 2023 at 11:08
  • $\begingroup$ What have tried? What is perplexing to you? $\endgroup$
    – Chaos
    Commented Oct 3, 2023 at 19:58
  • $\begingroup$ Well I've successfully managed to convert most of the code to binary but I'm not so sure about the REPEAT: commands and HLT. $\endgroup$
    – Hannah
    Commented Oct 5, 2023 at 7:18
  • $\begingroup$ I'm still lost as to why you can't just look at the output of a compiler run on your input. If in doubt about what it did, decompile the output. $\endgroup$
    – Kai
    Commented Oct 7, 2023 at 11:00

2 Answers 2

2
$\begingroup$

The process of converting assembly to actual machine code is carried out by the joint work of the assembler and the linker. The former is responsible for converting the assembly listing to object code, that is, actual instructions; the latter is responsible for linking together two or more objects and eventually produce the final executable code.

For answering your question, knowing what the assembler does is sufficient. It basically is responsible for translating the listing to a sequence of opcodes, while doing so it keeps track of the address of each instruction and is consequently responsible for resolving label names. Let's pretend the address of the first instruction was 0, then the address of repeat: corresponds to the size of all instructions starting from 0.

The listing you provided requires two passes to be assembled correctly (due to backreference); informally, you first emit all opcodes and mark the label as uncomputed and only once the lenght of the code following it is known the assembler decides what to write. You provided a listing employing the LOOP label instruction that translates to E2 cb, where cb is an 8-bit immediate value. (I am using this reference for translating.) Note that it must range over [-128,127], in case the number of bytes distantiating the LOOP instruction and the repeat: label was bigger than 128 the assembler would be unable to assemble and terminated with an error.

Let's say you had to jump 8 bytes backward using the LOOP instruction, you would translate it as E2 F8, that is E2 dec(-8).

$\endgroup$
1
$\begingroup$

You can launch an emulator (VirtualBox, VMware...) and run an old MS-DOS install.

You will be able to run the "debug" program which can assemble this code (just replace the REPEAT label with the address). "debug" can also execute that program step by step.

If you only want to see machine code, there are online assemblers/disassemblers which can automatically translate your code, for example : https://disasm.pro/ (select x86 16bits)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.