2

I'm completely new to Assembly and right now I'm using X86 assembly. I'm using the NASM and right now my code isn't compiling. I got it from a book and basically the code works with strings:

; This program demonstrates the string-handling procedures in
; the book's link library.
INCLUDE Irvine32.inc
.data
string_1 BYTE "abcde////",0
string_2 BYTE "ABCDE",0
msg0 BYTE "string_1 in upper case: ",0
msg1 BYTE "string1 and string2 are equal",0
msg2 BYTE "string_1 is less than string_2",0
msg3 BYTE "string_2 is less than string_1",0
msg4 BYTE "Length of string_2 is ",0
msg5 BYTE "string_1 after trimming: ",0
.code
main PROC
call trim_string
call upper_case
call compare_strings
call print_length
exit
main ENDP
trim_string PROC
; Remove trailing characters from string_1.
INVOKE Str_trim, ADDR string_1, '/'
mov edx,OFFSET msg5
call WriteString
mov edx,OFFSET string_1
call WriteString
call Crlf
ret
trim_string ENDP
upper_case PROC
; Convert string_1 to upper case.
mov edx,OFFSET msg0
call WriteString
INVOKE Str_ucase, ADDR string_1
mov edx,OFFSET string_1
call WriteString
call Crlf
ret
upper_case ENDP
compare_strings PROC
; Compare string_1 to string_2.
INVOKE Str_compare, ADDR string_1, ADDR string_2
.IF ZERO?
mov edx,OFFSET msg1
.ELSEIF CARRY?
mov edx,OFFSET msg2 ; string 1 is less than...
.ELSE
mov edx,OFFSET msg3 ; string 2 is less than...
.ENDIF
call WriteString
call Crlf
ret
compare_strings ENDP
print_length PROC
; Display the length of string_2.
mov edx,OFFSET msg4
call WriteString
INVOKE Str_length, ADDR string_2
call WriteDec
call Crlf
ret
print_leng

th ENDP
END main

Like I said I'm using NASM so this might be the problem but it should still work, but when i compile it using nasm -f win32 other.asm -o other.o it comes up witha plethora of errors most of which say parser instruction expected. I'm using a Windows 8 64 bit, but there's no reason why it can't run a 32 bit program- correct me if i'm wrong. The problem with the MASM compiler is that it says I need precisely Visual C++ Express 2005 (PRECISELY 2005) to download otherwise it doesn't download. How can I get this program to work along with other programs I might write in future- and I did remember to put the nasm assembler into the bin file of my C compiler. Like I said I'm fairly new and believe it or not the book doesn't actually tell you how to run the program. Also is there a way to download masm without VS 2005 (which i can't seem to find anyway) or any of the VS for that metter

Other programs (in ASM) don't seem to run on it either. I'm pretty sure this is the windows version otherwise it wouldn't have downloaded to begin with.

rkhb
  • 14,159
  • 7
  • 32
  • 60
  • 2
    If you're moving from one assembler (MASM) to a totally different one (NASM), no, it's not expected to "still work". The syntax of the assembler directives are different. The "instruction expected" error means that whatever it has run into isn't a valid NASM instruction. You'll need to convert. For example, in `nasm`, a file is included via the `%include` directive, not `INCLUDE` (note the presence of the percent sign). You can step through each parser error and pick out the instruction that has to change, for a starting point. – lurker Sep 25 '13 at 16:41
  • @mbratch Like I said I literally don't know any assembly, i'm just trying to get it all up and running before i even think of starting to program in it. as i want to follow my book, is there any may i could get NASM to go about this INCLUDE directive or is there another assembler i can download- i know about the MASM but i can't download for the reasons above –  Sep 25 '13 at 16:45
  • You could check here: http://left404.com/2011/01/04/converting-x86-assembly-from-masm-to-nasm-3/. I found this just by googling "convert masm to nasm". MASM is probably licensed free only to those who already own VS. – lurker Sep 25 '13 at 16:46
  • I just looked and MASM requires "Visual Studio Express 2005". I believe you can download the VS Express for free if you want to give that a go. – lurker Sep 25 '13 at 16:51
  • 2
    The problem is not that you're using Nasm, but that you're feeding it Masm code. What you probably want can be found at http://www.masm32.com There are alternatives... but beginners don't need any "extra" complications! No harm in using Masm - wash your hands afterwards! :) – Frank Kotler Sep 25 '13 at 18:50
  • @Frank Kotler that site only lets you download an SDK for MASM not the actual thing itself –  Sep 25 '13 at 19:49
  • I think that's what you want. I could be wrong. Japheth's Jwasm may also assemble that code. – Frank Kotler Sep 25 '13 at 20:00
  • 2
    @Kenny_007, the site Frank pointed you to, includes all of the MASM tools - ml, link, rc etc... So, yes it is the actual "Thing". I have been using the "SDK" for many years. – Gunner Sep 26 '13 at 03:27

2 Answers2

2

Why is my assembly program not working?

Because you're trying to compile assembler code in MASM syntax with NASM.

Option 1: get MASM
Do not try to put MASM code into NASM.
This will not work because every assembler has its own syntax.
(Yes, I agree that's messed up).

As per @Frank's suggestion download masm from: http://www.masm32.com/masmdl.htm
Note that the SDK (software development kit) is the actual thing with all the tools needed to compile code.
The masm installer rebuilds the development tools by recompiling them. This is somewhat unusual but it does ensure that all the tools needed to compile your code are present and working.

Option 2: use NASM source code examples
See: https://www.google.co.za/search?q=sample+nasm+programs&ie=utf-8&oe=utf-8&rls=org.mozilla:nl:official&client=firefox-a&gws_rd=cr&ei=uPNgUp-wBIqihgf45oDwCQ

Option 3: learn the differences between MASM and NASM
The nasm manual has a section on the differences with masm: http://www.nasm.us/doc/nasmdoc2.html#section-2.2
This might also be helpful: http://left404.com/2011/01/04/converting-x86-assembly-from-masm-to-nasm-3/

Option 4: get an auto-translator
Lucky for you there are auto-translators which will transform MASM code into NASM.
Here's one: http://www.devoresoftware.com/nomyso/
Note that this particular one requires perl.

Johan
  • 74,508
  • 24
  • 191
  • 319
0

Use the following code:

; String Library Demo   (StringDemo.asm)

; This program demonstrates the string-handling procedures in 
; the book's link library.

INCLUDE Irvine32.inc

.data
string_1 BYTE "abcde////",0
string_2 BYTE "ABCDE",0
msg0     BYTE "string_1 in upper case: ",0
msg1     BYTE "string1 and string2 are equal",0
msg2     BYTE "string_1 is less than string_2",0
msg3     BYTE "string_2 is less than string_1",0
msg4     BYTE "Length of string_2 is ",0
msg5     BYTE "string_1 after trimming: ",0

.code
main PROC

    call    trim_string
    call    upper_case
    call    compare_strings
    call    print_length

    exit
main ENDP

trim_string PROC
; Remove trailing characters from string_1.

    INVOKE Str_trim, ADDR string_1,'/'
    mov     edx,OFFSET msg5
    call    WriteString
    mov     edx,OFFSET string_1
    call    WriteString
    call    Crlf

    ret
trim_string ENDP

upper_case PROC
; Convert string_1 to upper case.

    mov     edx,OFFSET msg0
    call    WriteString
    INVOKE  Str_ucase, ADDR string_1
    mov     edx,OFFSET string_1
    call    WriteString
    call    Crlf

    ret
upper_case ENDP

compare_strings PROC
; Compare string_1 to string_2.

    INVOKE Str_compare, ADDR string_1, ADDR string_2
    .IF ZERO?
    mov edx,OFFSET msg1
    .ELSEIF CARRY?
    mov edx,OFFSET msg2     ; string 1 is less than...
    .ELSE
    mov edx,OFFSET msg3     ; string 2 is less than...
    .ENDIF
    call    WriteString
    call    Crlf

    ret
compare_strings  ENDP

print_length PROC
; Display the length of string_2.

    mov     edx,OFFSET msg4
    call    WriteString
    INVOKE  Str_length, ADDR string_2
    call    WriteDec
    call    Crlf

    ret
print_length ENDP

END main
Pang
  • 9,564
  • 146
  • 81
  • 122