I've done similar projects before, but i'm not sure what exactly you're having trouble with.
If I were building a slave program for a microprocessor (I use arduino and basic stamp) there would be a loop function that scans the IO pins, and all pin states would be assigned variables eg pin1 = REG(0), pin2 = REG(1) etc.
I find it simpler to create a few arrays :
and assign all similar IO types to the same array (all inputs together, all outputs together, all registers together)
The next step will be to process the incoming requests from the master so that the slave knows what (if any) data to respond with.
You can use hardware (such as a modbus shield), or software libraries (arduino has a few but I don't know about atmega32). . . or my favorite option - write your own code.
The modbus response packet has to be built based on what data was requested.
this link]1 was a help full site for learning to build the response packets.
The completed packets should look something like this
01 01 01 00 51 88 [coils]
01 02 01 00 A1 88 [inputs]
01 03 80 00 00 00... [holding registers]
The final step is to convert the above responses to ascii and send to the master (via rs232).
Also, I find it easier to write all my code to do calculations in 2 character hex format (00-FF) because it mimics the way data is handled in modbus. One analog register can store from 0-245, and registers are combined to store larger numbers.