News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Simple string swapping application. LNK1136 error

Started by Moddy, October 16, 2007, 05:02:24 AM

Previous topic - Next topic

Moddy

Ok, First up I'm VERY new to MASM - 2 hours into it actually. Actually finding it so interesting at the moment... Looks quite enjoyable =]

Heres my code..

.486                                           
.model flat, stdcall                           
option casemap :none                         

include \masm32\include\windows.inc       
include \masm32\include\kernel32.inc     
include \masm32\include\masm32.inc       

includelib \masm32\lib\kernel32.lib       
includelib \masm32\lib\masm32.lib         

.data
str1 db "String: ", 0
str2 db "One", 0
str3 db "Two", 0
newline db 13,10,"$"

.data?
strtemp db ?

.code
start:

invoke StdOut, addr str1
invoke StdOut, addr str2
invoke StdOut, addr newline

invoke StdOut, addr str1
invoke StdOut, addr str3
invoke StdOut, addr newline

;Code to swap the strings [We're putting str 2 into strtemp]
cld                          ; sets the direction flag to forward
mov esi, offset str2         ; move the source address in to esi
mov edi, offset strtemp      ; move the destination address in to edi
mov ecx, 3                   ; move the length to copy in to ecx
rep movsb                    ; copy length bytes from esi to edi

;Code to swap the strings [We're putting str3 into str2]
cld                          ; sets the direction flag to forward
mov esi, offset str3         ; move the source address in to esi
mov edi, offset str2         ; move the destination address in to edi
mov ecx, 3                   ; move the length to copy in to ecx
rep movsb                    ; copy length bytes from esi to edi

;Code to swap the strings [We're putting strtemp into str3]
cld                          ; sets the direction flag to forward
mov esi, offset strtemp      ; move the source address in to esi
mov edi, offset str3         ; move the destination address in to edi
mov ecx, 3                   ; move the length to copy in to ecx
rep movsb                    ; copy length bytes from esi to edi

invoke StdOut, addr str1
invoke StdOut, addr str2
invoke StdOut, addr newline

invoke StdOut, addr str1
invoke StdOut, addr str3
invoke StdOut, addr newline

invoke ExitProcess, 0                                   

end start


And heres what happens when I attempt to assemble and link:

QuoteC:\Documents and Settings\Administrator\My Documents\Code>\masm32\bin\ml /c /Zd /coff chal1.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: chal1.asm

C:\Documents and Settings\Administrator\My Documents\Code>\masm32\bin\link.exe /SUBSYSTEM:CONSOLE chal1.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

chal1.asm : fatal error LNK1136: invalid or corrupt file

C:\Documents and Settings\Administrator\My Documents\Code>

I've googled LNK1136 and can't find any relevant information. Anybody know whats wrong? Any suggestions or advice are really appreciated..

Thanks in advance!

BogdanOntanu

You must LINK the .OBJ file resulted from the process of assembly not the source .ASM file.
LINK expects and .OBJ format (either OMF or COFF) and not a text file as input. ;)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Tedd

ML takes an .asm file and outputs an .obj file.
LINK takes .obj files and outputs an .exe file.


Just pass the output of ml ("chal1.obj", not "chal1.asm") to link instead :wink
No snowflake in an avalanche feels responsible.

Moddy

Thankyou so much guys, I hadn't noticed that I was supplying the linker with the .asm file.   :red

Teaches me not be lazy and rely on tab-completion - Or I probably would've seen it.. In my defence, it was 6AM! :P

Mark Jones

Hi Moddy, welcome to the fourm. :U A few quick notes about the code:

Quote from: Moddy on October 16, 2007, 05:02:24 AM

.data
str1 db "String: ", 0
str2 db "One", 0   ;<--- when copying str3 to str2, str2 must be large enough to take all of str3
                   ;(which it is here, both 4 bytes, but this will not always be the case.)
str3 db "Two", 0
newline db 13,10,"$"  ;<--- all strings must end in a null (0) - the "$" is from the old dos days
; the only reason this doesn't crash is because there are nulls present after this string by default.

.data?
strtemp db ?  ;<--- this must be big enough to contain all of the copied string, see below.


Elements in the .data section are literal data, which is embedded inside the executable at assemble-time. Elements in the .data? section however, are (uninitialized) placeholders for memory addresses defined at run-time. What this means is, you can have the program make buffers in memory at run-time, and not bloat your executable with a bunch of empty space. All bytes in the .data? section are initialized to zero (nulls.) Consider something like the following:


.data
str1 db "String: ", 0   ; embedded into executable
str2 db "One", 0
str3 db "Two", 0
newline db 13,10,0

.data?
strtemp db 128 dup(?)   ; larger temporary buffers, defined at run-time
newstr1 db 128 dup(?)   ; these can hold any combination of the above string literals
newstr2 db 128 dup(?)   ; experiment :-)
newstr3 db 128 dup(?)


Here we have the contents of our data strings embedded in the executable. Now we can copy and manipulate these into the larger memory areas, without fear of accidentally overwriting other nearby strings.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Vortex

The Masm32 package provides two MS COFF linkers : The Microsoft linker link.exe and Pelle'S polink.exe

Both they can be found at :

\masm32\bin

link /SUBSYSTEM:WINDOWS objectfile.obj

or

link /SUBSYSTEM:CONSOLE objectfile.obj

Moddy

Quote from: Mark Jones on October 16, 2007, 03:57:25 PM
Hi Moddy, welcome to the fourm. :U A few quick notes about the code:

Quote from: Moddy on October 16, 2007, 05:02:24 AM

.data
str1 db "String: ", 0
str2 db "One", 0   ;<--- when copying str3 to str2, str2 must be large enough to take all of str3
                   ;(which it is here, both 4 bytes, but this will not always be the case.)
str3 db "Two", 0
newline db 13,10,"$"  ;<--- all strings must end in a null (0) - the "$" is from the old dos days
; the only reason this doesn't crash is because there are nulls present after this string by default.

.data?
strtemp db ?  ;<--- this must be big enough to contain all of the copied string, see below.


Elements in the .data section are literal data, which is embedded inside the executable at assemble-time. Elements in the .data? section however, are (uninitialized) placeholders for memory addresses defined at run-time. What this means is, you can have the program make buffers in memory at run-time, and not bloat your executable with a bunch of empty space. All bytes in the .data? section are initialized to zero (nulls.) Consider something like the following:


.data
str1 db "String: ", 0   ; embedded into executable
str2 db "One", 0
str3 db "Two", 0
newline db 13,10,0

.data?
strtemp db 128 dup(?)   ; larger temporary buffers, defined at run-time
newstr1 db 128 dup(?)   ; these can hold any combination of the above string literals
newstr2 db 128 dup(?)   ; experiment :-)
newstr3 db 128 dup(?)


Here we have the contents of our data strings embedded in the executable. Now we can copy and manipulate these into the larger memory areas, without fear of accidentally overwriting other nearby strings.

Thanks for the welcome!  :bg

About the newline - I admit I was clueless as to how to start a newline in ASM. Coming from a C/C++ back ground I've been used to "\n" or "endl" - So I was stumped this morning. A quick google gave me that - upon closer inspection I should've known it was for old dos, by the excessive interrupts.

About the string's - Yeah, I assume a buffer overflow is really easy to produce in ASM, So thanks for the advice about the strings and memory allocation.

Vortex: Thanks for that, I didn't know it had polink.exe - Is there any reasoning behind having 2 different linkers?

jdoe

Quote from: Moddy on October 16, 2007, 11:36:35 PM
Thanks for that, I didn't know it had polink.exe - Is there any reasoning behind having 2 different linkers?

Some prefer "McIntosh" and some "Granny Smith" but at the end, they are all apples.

:P


Mark Jones

Polink.exe can generally create slightly smaller executables. But they both function nearly identical, try both and see which one you like best. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08