开发者

Assembling source code from a text file

开发者 https://www.devze.com 2023-01-15 13:51 出处:网络
Okay, I am currently in an Assembly 开发者_开发问答Languages class at school.We are delving into some source code this week for the first time.My teacher has given us an example as follows:

Okay, I am currently in an Assembly 开发者_开发问答Languages class at school. We are delving into some source code this week for the first time. My teacher has given us an example as follows:

;plan for getting a, b, c, and displaying ab + bc
call getVal a
mov M1, AX 
call getVal 
mov M2, AX 
call getVal
mov BX, AX 
mul M2
mov CX, AX 
mov AX, M2 
mul M1 
add AX, CX 
call putVal 

He has told us that we need to save this in a .txt file then from there convert it into a binary executable file to run it. What's the best way to get this done? Can it be done through the command line?

EDIT: sorry the code didn't come out just right, but it isn't necessarily important what it does. I just need to know how to run any source code that is saved as a .txt file

EDIT: Thanks Null


Assembly language file usually have .s .S or .asm but not .txt even it doesn't matter.

As asked, you should precise a bit your question giving the machine type and toolchain.

This really looks like an Intel-mnemonic assembly using Intel assembly (vs AT&T, see Wikipedia x86 assembly for a quick overview).

You can try NASM (The Netwide Assembler) to compile such source code.

Of course, some parts are missing in your file as stated by NASM (under Linux).

$ cat file.s
; comment
section .text
    global fct
fct:
    call getVal
    mov M1, AX 
    call getVal 
    mov M2, AX 
    call getVal
    mov BX, AX 
    mul M2
    mov CX, AX 
    mov AX, M2 
    mul M1 
    add AX, CX 
    call putVal
$ nasm -f elf -o file.o file.s
file.s:5: error: symbol `getVal' undefined
file.s:6: error: symbol `M1' undefined
file.s:7: error: symbol `getVal' undefined
file.s:8: error: symbol `M2' undefined
file.s:9: error: symbol `getVal' undefined
file.s:11: error: symbol `M2' undefined
file.s:13: error: symbol `M2' undefined
file.s:14: error: symbol `M1' undefined
file.s:16: error: symbol `putVal' undefined

Note that I used ELF output format as it helps using GNU objdump. But you can choose another output fomat within the following list (nasm -hf):

valid output formats for -f are (`*' denotes default):
  * bin       flat-form binary files (e.g. DOS .COM, .SYS)
    ith       Intel hex
    srec      Motorola S-records
    aout      Linux a.out object files
    aoutb     NetBSD/FreeBSD a.out object files
    coff      COFF (i386) object files (e.g. DJGPP for DOS)
    elf32     ELF32 (i386) object files (e.g. Linux)
    elf       ELF (short name for ELF32) 
    elf64     ELF64 (x86_64) object files (e.g. Linux)
    as86      Linux as86 (bin86 version 0.3) object files
    obj       MS-DOS 16-bit/32-bit OMF object files
    win32     Microsoft Win32 (i386) object files
    win64     Microsoft Win64 (x86-64) object files
    rdf       Relocatable Dynamic Object File Format v2.0
    ieee      IEEE-695 (LADsoft variant) object file format
    macho32   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files
    macho     MACHO (short name for MACHO32)
    macho64   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
    dbg       Trace of all info passed to output stage

For MS users, you could have used MASM or TASM which use another assembly-style code:

; comment
.CODE
fct:
   [...]
END

Hope it helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号