8 Replies - 1022 Views - Last Post: 12 May 2013 - 01:26 AM

#1 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

2019 Link error VS 2012

Posted 10 May 2013 - 04:11 PM

Wondering if I can get some help. Working on an example problem using Kip Irvine Library in Visual Studio '12 so MASM.

First time working with INVOKE, PROC, and PROTO directives, but I don't understand this error message as I've never gotten it before. All my other assembly programs work
except for when I try to invoke to another procedure.

Any ideas?

The library is here:
http://www.asmirvine.com/

here is my code:
     TITLE  test procedure      (test.asm) 

    COMMENT ! Create two procedures: (1) SetColor receives two byte parameters: forecolor and backcolor. 
    It calls the SetTextColor procedure from the Irvine32 library. (2) WriteColorChar receives three byte 
    parameters: char, forecolor, and backcolor. It displays a single character, using the color attributes
    specified in forecolor and backcolor. It calls the SetColor procedure, and it also calls WriteChar
    from the Irvine32 library. Both SetColor and WriteColorChar must contain declared parameters. Write
    a short test program that tests both procedures. Be sure to create PROTO dec- larations for SetColor 
    and WriteColorChar.
    ! 



    INCLUDE Irvine32.inc

    SetColor PROTO, forecolor:BYTE, backcolor:BYTE   

    .data
    prompt BYTE "This program will change text color.", 0
    color1 BYTE "White"
    color2 BYTE "Black" 

    .code 
    main PROC
	
	; make sure screen is clear
	call Clrscr
	
	; Display program details 
	mov edx, OFFSET prompt
	call WriteString
	call Crlf 
	
	; Pass parameters to SetColor procedure to change text color
	INVOKE SetColor, color1, color2
	
	; Write string to see colors
	call WriteString
	
	exit
    main ENDP
    END main 


    SetColor PROC forecolor:BYTE, backcolor:BYTE
	
	
	
	mov eax, forecolor + (backcolor SHL 4) 
	call SetTextColor
	
	ret

    SetColor ENDP 





This is the error msg:

------ Build started: Project: Project, Configuration: Debug Win32 ------
test8.obj : error LNK2019: unresolved external symbol _SetColor@8 referenced in function _main@0
C:\Irvine\Projects\Procedures\Debug\Project.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Is This A Good Question/Topic? 0
  • +

Replies To: 2019 Link error VS 2012

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: 2019 Link error VS 2012

Posted 10 May 2013 - 08:53 PM

Irvine's book is good, but his coding practices are a bit off. Your problem is what many folks have with his way of coding.

Your problem is because your SetColor proc is OUTSIDE of the code section!!! The code Kip uses is like this:

.code
Main proc       ; program entry point
    
    ; code here
    ret

Main endp       ; End of Main proc

    ; more code/ procs here
    
end Main        ; End of code section


This confuses many people. The standard way of doing it, no matter the Assembler is:
.code
Main:
    ; Startup code here

    ; your procs here
    
end Main


Main is the entry point of the exe (offset address), 99% of the time, Main can be called anything you want, Start, ExeName; as long as the last line is end EntryPointName

No matter which way you do it, ALL code MUST be between the EntryLabel and end Entrylabel. So, go ahead and move your SetColor proc between Main endp and end Main

Once you do that, your link error will be gone, but you should get another error because mov eax, forecolor + (backcolor SHL 4) is not valid syntax.

Your variables color1 and color2 are string arrays, and you are passing a byte to your proc, so you are passing "W" as the forecolor and "B" as the backcolor which in actuality you are passing 87 and 66 - ASCII codes for W and B.

Colors are the following:
    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

You could also do this with equates.

ccRed equ 4
ccLghtYellow equ 0EH

INVOKE  SetColor, 0Fh, 4  ; white on red
call    WriteString
call Crlf 

INVOKE  SetColor, ccRed, ccLghtYellow  ; red on bright yellow
call    WriteString


Attached Image

Your SetColor Proc should be:
SetColor PROC forecolor:BYTE, backcolor:BYTE
    movzx   eax, backcolor 
    shl     eax, 4
    or      al, forecolor
    call    SetTextColor
    ret
SetColor ENDP 

Was This Post Helpful? 1
  • +
  • -

#3 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: 2019 Link error VS 2012

Posted 10 May 2013 - 09:13 PM

Wow thats amazing. The funny thing is I have been putting all my procedures outside of main for the entire semester (habit from C++ coding) but the standard "call" will access it. I am guessing the MASM directive for handling INVOKE and PROTO specifies it in main. I was getting frustrated with this as I kept going back and forth in the chapter and noticed Irvine never has a complete segment of code that details INVOKE and PROC usage. Only in mixed module format (which for simple programs is overkill).

thank you so much!

For the second part, I wasn't sure how to pass the info as bytes as the problem wanted. Didn't realize I didn't have to have variables and just send immediate values. I just used the constant inter values in the other problems that needed to change the color.

I get the movzx (since byte must be extended into EAX and INVOKE can erase it)..but why in the SetColor PROC did you or al, forecolor? I don't remember learning that OR will add a register value!
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: 2019 Link error VS 2012

Posted 10 May 2013 - 09:30 PM

The color for console is an 8 bit number WORD sized. 0 - 3 is the foreground, and 4 - 7 is the background. Each bit has a meaning, but for this we are only interesting in setting the color.

Red background, white text:
move red into eax zeroing out the unused bits
movzx eax, backcolor = 4 = 0000 0100

Now we need to get 0100 into bits 4 - 7 so we shift left by 4
shl eax, 4 - eax == 0100 0000 = 64 (actually AX)

Now we need to get the foreground into bits 0 - 3 without disturbing 4 - 7
or al, forecolor -
white = 0Fh = 0000 1111
red = 0100 0000

     0100 0000
OR   0000 1111
------------
AX = 0100 1111


OR Truth table
Was This Post Helpful? 0
  • +
  • -

#5 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: 2019 Link error VS 2012

Posted 10 May 2013 - 10:27 PM

Interesting.

The book just gives the formula as frontcolor + (backcolor x 16). Now I see what it actually means.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: 2019 Link error VS 2012

Posted 11 May 2013 - 11:44 AM

I decided to get this book sometime ago to see what everyone was having problems with. He does use frontcolor + (backcolor * 16) but fails to mention how to use it. You use it as an equate:
ccRed           equ     4
ccLghtYellow    equ     0EH
RED_ON_YELLOW   equ     ccRed + (ccLghtYellow * 16)

And with that, you call SetTextColor like this:
    mov     eax, RED_ON_YELLOW
    call    SetTextColor



OR:
ccRed           equ     4
ccLghtYellow    equ     0EH
RED_ON_YELLOW   equ     ccRed + (ccLghtYellow SHL 4)


The code he uses and the way I shown, are 2 different beasts.
Both of these ways are Assemble time constants. RED_ON_YELLOW is computed when code is Assembled, if that is what you want then fine, but you are stuck with those values. The way I showed, you can have the user enter 2 numbers for the fore and back colors, work the magic and the color changes.
Was This Post Helpful? 0
  • +
  • -

#7 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: 2019 Link error VS 2012

Posted 11 May 2013 - 03:52 PM

I did this instead that will handle constant integers


    SetColor PROC forecolor:BYTE, backcolor:BYTE
	
	movzx eax, backcolor 
	shl eax, 4 
	movzx ebx, forecolor
	add eax, ebx
	call SetTextColor
	
	ret
    SetColor ENDP




This way I can just validate user input with integers 0-F :)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: 2019 Link error VS 2012

Posted 11 May 2013 - 06:16 PM

Well, ADDing is NOT the same as ORing!!! You are not adding numbers together, but fiddling bits. This might work fine here, but what about other cases? If you get into the habit of using ADD, you will have problems when you work with 32bit colors, or other numbers where each bit has a meaning.

You don't need to use movxz 2 times, it is "expensive"

This will do the same thing:
    xor     eax, eax
    mov     al, backcolor 
    shl     al, 4
    or      al, forecolor
    call    SetTextColor


Do you understand what is going on in this code? Do you understand the sizes of EAX, AX, AH, AL? Their position in the EAX register?
Was This Post Helpful? 0
  • +
  • -

#9 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: 2019 Link error VS 2012

Posted 12 May 2013 - 01:26 AM

Yes I do. It's just that I am taking an online class and the emphasis isn't on optimization. I can only go by the Irvine library and I'm not going to delve into the documentation to find out the specific logic implementation of the code. This is my first assembly class. I will have many more as an EE student. I understand bit masking but haven't really dealt with homework assignments that actually use it.

thanks for the explanation though. I understand completely why you are telling me the approach you gave me the first time. I'm not a fan of this book. But I will learn NASM next year in more depth for a better technical understanding :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1