2010-09-08 55 views
0

好的,我目前在學校的彙編語言課上。我們本週第一次深入研究一些源代碼。彙編來自文本文件的源代碼

;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 

他已經告訴我們,我們需要把它保存在一個.txt文件,然後從那裏將它轉換成二進制可執行文件來運行它:我的老師如下給我們作出了榜樣。完成這件事的最好方法是什麼?可以通過命令行完成嗎?

編輯:對不起,代碼沒有出來恰到好處,但它並不一定重要。我只需要知道如何運行另存爲.txt文件

編輯任何源代碼:感謝空

+0

嘗試http://flatassembler.net/index.php – NullUserException 2010-09-08 17:06:11

+0

你應該使用什麼工具鏈?通常你需要做的就是將代碼傳遞給你的彙編器和鏈接器,並且你會得到一個可執行文件。你有沒有試過問你的教授? – 2010-09-08 17:07:09

+0

我給他發了一封電子郵件,他似乎是一個慢響應者。你在「彙編和鏈接器」中失去了我,哪裏可以獲得?一旦我擁有可執行文件,我該做什麼? – javaTheHut 2010-09-08 17:16:22

回答

1

彙編語言文件通常有.s.S.asm但不.txt甚至也沒關係。

正如問道,您應該精確一點,提供機器類型和工具鏈。

這實際上看起來像是一個使用Intel組件的Intel助記憶組件(vs AT & T,參見Wikipedia x86 assembly以獲得快速概述)。

你可以試試NASM (The Netwide Assembler)來編譯這樣的源代碼。

當然,您的文件中缺少某些部分,如NASM所述(在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 

請注意,我用ELF輸出格式,因爲它有助於使用GNU objdump。但是你可以選擇以下列表中的其他輸出fomat(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 

對於MS的用戶,你也可以使用它使用另一種彙編代碼風格MASMTASM

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

希望它幫助:)