I use visual studio 2017
Lets say I one assembly file named "Factorial.asm" and I break it into two .asm files named "one.asm" and "two.asm":
Factiorial.asm works just fine.
Factorial.asm contains
.386
.model flat, stdcall
option casemap :none
includelib \masm32\lib\msvcrt.lib
sprintf proto C :vararg
includelib \masm32\lib\user32.lib
MessageBoxA proto :ptr,:ptr,:ptr,:DWORD
includelib \masm32\lib\kernel32.lib
ExitProcess proto :dword
.data
format db "%llu", 13, 10, 0
_title db "Result",13,10,0
.code
main PROC
LOCAL szBuf[9]:byte
mov eax, 15 ; initial value (low-order bits)
xor edx, edx ; initial value's high-order bits are 0
mov ecx, eax ; loop counter
Factorial:
dec ecx ; decrement counter
jz Finished ; when counter == 0, we're done
mov ebx, ecx ; make copy of counter
imul ebx, edx ; high-order bits * multiplier
mul ecx ; low-order bits * multiplier
add edx, ebx ; add high-order product to high-order bits of low-order product
cmp ecx, 1
jg Factorial ; keep looping as long as counter > 1
Finished:
invoke sprintf, addr szBuf, offset format, eax, edx
invoke MessageBoxA, 0, addr szBuf, offset _title, 0
invoke ExitProcess, 0
main ENDP
one.asm contains
.386
.model flat, stdcall
option casemap :none
includelib \masm32\lib\msvcrt.lib
sprintf proto C :vararg
includelib \masm32\lib\user32.lib
MessageBoxA proto :ptr,:ptr,:ptr,:DWORD
includelib \masm32\lib\kernel32.lib
ExitProcess proto :dword
.data
format db "%llu", 13, 10, 0
_title db "Result",13,10,0
.code
main PROC
LOCAL szBuf[9]:byte
mov eax, 15 ; initial value (low-order bits)
xor edx, edx ; initial value's high-order bits are 0
mov ecx, eax ; loop counter
Factorial:
dec ecx ; decrement counter
jz Finished ; when counter == 0, we're done
mov ebx, ecx ; make copy of counter
imul ebx, edx ; high-order bits * multiplier
mul ecx ; low-order bits * multiplier
add edx, ebx ; add high-order product to high-order bits of low-order product
cmp ecx, 1
jg Factorial ; keep looping as long as counter > 1
main ENDP
two.asm contains
Finished:
invoke sprintf, addr szBuf, offset format, eax, edx
invoke MessageBoxA, 0, addr szBuf, offset _title, 0
invoke ExitProcess, 0
How would I link "one.asm" and "two.asm" using Visual Studio 2017. Or in other words, call labels from separate .asm files?
Use the directive extrn to declare functions outside of the current source file:
extrn foo:proc
You can optionally use public to declare local functions as public, but I think functions are public by default.
Since VS2015, printf is now inlined with C / C++ code, at least in the case of 64 bit builds. One way to deal with this is to have a C / C++ source file that makes a reference to printf, in which case the assembly code can then access printf. I don't know if this applies to sprintf also. You'll get a link error if this is an issue.
There are usually three types of objects that you need to transfer information across files to.
Proc defined in source file called for in user file : By default, in masm, all procs are global. So, the source file needs no declaration, the user file needs a declaration :
extern <proc_name> : proc
Variable defined in source file and called by a procedure in user file : In this situation, the source file needs a declaration
public <var_name>
and the user file must contain the declaration :
extern <varname> : var_size ; where var_size is word, qword ymmword...
It is NOT mandatory that the declaration size must match the extern size. However, consequences of not ensuring this match are usually expensive. Also, if a variable has been declared as an extern, and subsequently, not used in that file, it still must match a corresponding public declaration in some file, otherwise linker will fail.
Label defined in source file and jumped to from a user file : identical treatment as a variable (case 2) is treated
Trust this helps.
Related
I am using visual studio for assembly coding and I am pretty new to assembly. My problem is when I assign a number to a register the value of the number is changed.
.386
.model flat, stdcall
stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
;define your variables here
adword DWORD ?
aword WORD 7788h
.code
main PROC
mov eax,11223344h ; moved a constant into a register
mov ebx, eax ; moved ebx -> eax
mov adword, eax ; mov eax-> adword
mov ax, aword ; 7788h -> ax
mov ah, 10h
INVOKE ExitProcess, 0
main ENDP
END main
The value of Eax is not 11223344 it becomes something else.
Beginner level in Assembly.
The error I receive in Visual Studio is:
1>File2.asm(27): error A2006: undefined symbol : sprintf
1>File2.asm(28): error A2006: undefined symbol : MessageBoxA
File 1 is what handles calculations
File 2 is what prints result to a window.
The line the handles print instructions is:
invoke sprintf, addr szBuf, offset $interm, eax, edx
invoke MessageBoxA, 0, addr szBuf, offset _title, 0
invoke ExitProcess, 0
What am I doing wrong to the cause it not to build?
Is it because sprintf is a C function?
File1.asm
.386
.model flat, stdcall
option casemap :none
PUBLIC squareroot
PUBLIC szBuf
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\msvcrt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\msvcrt.lib
.data
_title db "Result",13,10,0
$interm db "%0.4f","+","%0.5f",13,10,0
Aval REAL8 1.000
Bval REAL8 -2.000
Cval REAL8 19.000
_fourval REAL8 4.000
$Tvalueinc REAL4 1.0,2.00,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0
$sampleval real10 4478784.0
$Powercounter dd ?
squareroot dq ?
$prevCW dw ?
$Tagword dd ?
$INT1 dq ?
EXTERN Finished:PROC
.code
szBuf:
add eax,4
fstcw $prevCW
fwait
fld Bval ; [loads first instance of b]]
fmul Bval ; [b*b = b^2]
fld Aval ;[Load a (a*c)]
fmul Cval ;(a*c)
fmul _fourval ;[4*a*c]
fsubp;[b^2-4*a*c]
ftst ;compare ST(0) with 0.0
fstsw ax ;[store camparison results in ax]
sahf ;transfer flags from AH register
mov ecx, 0004h
jb _negative ;jump if <0
fsqrt ;sqrt(b^2-4*a*c)
_negative:
fchs
fsqrt
fld $sampleval
xor eax,eax
$repeat:
inc eax
push eax
mov ax, $prevCW
push eax
fldcw [esp]
fld $Tvalueinc[ecx]
fdivp
fld st(0)
FRNDINT
fcomp
fstsw ax
Sahf
fnstenv [ebx-10h]
movzx eax, word ptr [ebx-10h + 8h]
fldcw $prevCW
pop eax
pop eax
jz $repeat
dec eax
cmp eax, $Powercounter
add ecx, 0004h
mov eax, dword ptr squareroot
mov edx, dword ptr squareroot[0004h]
jmp Finished
END szBuf
File2.asm
.386
.model flat,stdcall
option casemap:none
PUBLIC Finished
PUBLIC ExitProcess
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\msvcrt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\msvcrt.lib
.data
_title db "Result",13,10,0
$interm db "%0.4f","+","%0.5f",13,10,0
.code
Finished:
invoke sprintf, addr szBuf, offset $interm, eax, edx
invoke MessageBoxA, 0, addr szBuf, offset _title, 0
invoke ExitProcess, 0
END
You are using the function sprintf from MSVCRT.lib, which is a C library, and its exported name is prefixed by an underscore. Hence it's _sprintf instead of sprintf.
The function MessageBox is contained in user32.lib which you didn't incluce, so the linker could not find it.
There's also the function wsprintf in user32.lib which is quite similar to sprintf, so if you want to save space and decrease the size of the file you can use that one instead.
sprintf and wsprintf both use the C calling convention (contrary to the STDCALL convention set as default in the .model flat,stdcall line).
Note It is important to note that wsprintf uses the C calling convention (_cdecl), rather than the standard call (_stdcall) calling convention. As a result, it is the responsibility of the calling process to pop arguments off the stack, and arguments are pushed on the stack from right to left. In C-language modules, the C compiler performs this task.
But INVOKE(more precise: its PROTO directive) does take care of that so don't worry now.
To fix the errors change/add these lines to your code:
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
...
invoke _sprintf, addr szBuf, offset $interm, eax, edx
This is my code, the program runs fine when I define the third variable as cc, if and when I change it to C, the variable turns blue (indicating that it is a keyword in the assembly language since I have the .dat file installed to highlight assembly language keywords) the debugger gives me an error of ...\Irvine\Examples\ch03\AddSums.asm" exited with code 1.
I have ran the code through breakpoints in the first case too. Everything is good. I am just concerned since this is an assignment for school and the variable needs to be C, why is it not letting me?
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
A DWORD 30
B DWORD 20
cc DWORD 10
D DWORD 5
.code
main PROC
mov eax, A
mov ebx, B
mov ecx, cc
mov edx, D
add eax, ebx
add ecx, edx
sub eax, ecx
mov A, eax
INVOKE ExitProcess, 0
main ENDP
END main
Comment #
Using Programming Exercise 6 in Chapter 4 as a starting point,
write a program that generates the first 47 values in the Fibonacci
series, stores them in an array of doublewords, and writes the
doubleword array to a disk file.
#
INCLUDE c:\Irvine\Irvine32.inc
FIB_COUNT = 47 ; number of values to generate
.data
fileHandle DWORD ?
filename BYTE "fibonacci.bin",0
array DWORD FIB_COUNT DUP(?)
.code
main2sub PROC
; Generate the array of values
mov esi, OFFSET array
mov ecx, LENGTHOF array
call generate_fibonacci
; Create the file, call CreateOutputFile
mov edx,OFFSET filename
call CreateOutputFile
mov fileHandle, eax
; Write the array to the file, call WriteToFile
mov eax, fileHandle
mov edx, OFFSET array
mov ecx, FIB_COUNT * 4
call WriteToFile
; Close the file, call CloseFile
mov eax, fileHandle
call CloseFile
exit
main2sub ENDP
;------------------------------------------------------------
generate_fibonacci PROC USES eax ebx ecx edx
;
; Generates fibonacci values and stores in an array.
; Receives: ESI points to the array,
; ECX = count
; Returns: nothing
;------------------------------------------------------------
mov ebx, 1
mov ecx, 0
L1:
add ebx, ecx
mov [esi], eax
call WriteDec
call Crlf
inc esi
xchg ebx, eax
loop L1 ;end of looping
ret
generate_fibonacci ENDP
END main2sub
It is my first semester learning about Assembly Language. Im not sure what to do with this error message "fatal error LNK1120: 1 unresolved externals". Can anyone help me out?
The linker couldn't find the library-files Irvine32.lib, Kernel32.Lib and/or User32.Lib. They are in the same folder as Irvine32.inc
The easiest way is to inform ML about those libraries with a MASM-directive. Insert following three lines right behind the INCLUDE c:\Irvine\Irvine32.inc:
INCLUDELIB c:\Irvine\Irvine32.lib
INCLUDELIB c:\Irvine\Kernel32.lib
INCLUDELIB c:\Irvine\User32.lib
I'm one day into learning ASM and I've done a few tutorials, and even successfully modified the tutorial content to use jmp and cmp, etc instead of the MASM .if and .while macros.
I've decided to try and write something very, very simple to begin with before I continue with more advanced tutorials. I'm writing a Fibonacci number generator. Here is the source I have so far:
.386
.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
.code
start:
mov eax, 1
mov ecx, 1
_a:
push eax
add eax, ecx
pop ecx
; Jump to _b if there is an overflow on eax
; Print Values Here
jmp _a
_b:
push 0
call ExitProcess
end start
I intend to check for overflows on eax/ecx but right now I'm just interested in displaying the values of eax/ecx on the screen.
I know how to push the address of a constant string from .data and call StdOut which was the first example in the hello world tutorial, but this appears to be quite different (?).
There is this code provided by Microsoft itself
http://support.microsoft.com/kb/85068
Note that this code outputs AX register on 16 bit systems. But you can get the idea, you just need to convert AX value into ASCII characters by looping through each character. Skip the interrupts part and use your StdOut function.
mov dx, 4 ; Loop will print out 4 hex characters.
nexthex:
push dx ; Save the loop counter.
mov cl, 4 ; Rotate register 4 bits.
rol ax, cl
push ax ; Save current value in AX.
and al, 0Fh ; Mask off all but 4 lowest bits.
cmp al, 10 ; Check to see if digit is 0-9.
jl decimal ; Digit is 0-9.
add al, 7 ; Add 7 for Digits A-F.
decimal:
add al, 30h ; Add 30h to get ASCII character.
mov dl, al
;Use StdOut to print value of dl
;mov ah, 02h ; Prepare for interrupt.
;int 21h ; Do MS-DOS call to print out value.
pop ax ; Restore value to AX.
pop dx ; Restore the loop counter.
dec dx ; Decrement loop counter.
jnz nexthex ; Loop back if there is another character
; to print.
See here as well:
http://www.masm32.com/board/index.php?PHPSESSID=fa4590ba57dbaad4bc44088172af0b49&action=printpage;topic=14410.0