Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

03-22-2017 , 10:13 AM
Quote:
Originally Posted by KatoKrazy
Hard to trace through code without looking through what it is supposed to do when taking a written exam

You are correct, main should return an int.
I think it's valid to use void type like in the code, just unusual, because a program can grow up and become a process one day, and it returns a 1 if an error is raised, and 0 otherwise. Not a code historian though.
Programming homework and newbie help thread Quote
03-25-2017 , 12:26 PM
What are the good assembly language books for beginners? I've found a few via google but I'm curious if anyone here has some good resources. It's a bit confusing for a newbie because of all the different acronyms that i don't fully understand yet.
Programming homework and newbie help thread Quote
03-25-2017 , 01:54 PM
IBM/Intel assembly or other?
Programming homework and newbie help thread Quote
03-25-2017 , 02:21 PM
I'm not entirely equipped to answer that question.
Programming homework and newbie help thread Quote
03-25-2017 , 03:40 PM
It's an important question. There are many flavors of assembly out there, each with their own syntax and instruction sets. You need to know which compiler/toolchain you're using.

edit: for example, I recently took an assembly file designed for an ARM compiler and wanted to build it on a GNU compiler with ARM option. I had to make changes to over half the lines of code.

Last edited by Lattimer; 03-25-2017 at 03:57 PM.
Programming homework and newbie help thread Quote
03-25-2017 , 05:12 PM
I'll mostly be working with Linux-based desktop/server programs afaik so I'd guess intel
Programming homework and newbie help thread Quote
03-25-2017 , 05:41 PM
I can't recommend the CMU 15-213 enough. You can find the lectures online taught by the authors of the textbook.
Programming homework and newbie help thread Quote
03-31-2017 , 09:18 AM
Which ide do people recommend for cpp?
Programming homework and newbie help thread Quote
03-31-2017 , 07:20 PM
I use what I assume is a very similar product of theirs at work (AppCode, which only runs on Mac and adds Objective-C support) and love it. I've always like Visual Studio too, if on Windows.
Programming homework and newbie help thread Quote
03-31-2017 , 10:34 PM
Quote:
Originally Posted by CyberShark93
Which ide do people recommend for cpp?
Spoiler:


/troll
Programming homework and newbie help thread Quote
04-04-2017 , 12:01 PM
I'm not sure if this is the right thread or not, but it seems like it is close.

I don't have any work at the moment (which is bad) so I've decided to learn some python in the downtime. I also decided to do this in linux as I have no experience in linux as I actively avoided it in University. With that said I installed Linux Mint in a VM that I'm running in Windows 10, and then I installed Spyder and off I went going through some tutorials and getting my feet wet with some syntax. I'm not a noob and have experience with C#, C, C++, Visual Basic, etc., but not on a professional scale. I've programmed some small apps for work and personal projects.

So, I am going through Data Science from Scratch and I type up the following:
Code:
from matplotlib import pyplot as plt

years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]

#create a line chart, years on x-axis, gpd on y-axis
plt.plot(years, gdp, color='green', marker = 'o', linestyle = 'solid')

#add a title
plt.title("Nominal GDP")

#add a label to the y-axis
plt.ylabel("Billions of $")
plt.show()
And when I run it inside of the Spyder IDE I get this error:
Code:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
    builtins.execfile(filename, *where)
  File "/home/mdodd/markstuff/DataScience From Scratch/chapter3_1.py", line 9, in <module>
    from matplotlib import pyplot as plt
ImportError: No module named matplotlib
The first thing I did was decided that matplotlib didn't exist so I remembered that the book recommended I install anaconda, so I did that, and I tested it, and it is installed. I then ran the code in Spyder again and got the same error. I messed around with some stuff and then I went to the terminal and ran the code from the terminal with:
Code:
python chapter3_1.py
And it worked as expected. So now I know I have the libraries installed correctly and that the code is correct and it's something that is wrong with the Spyder IDE, but I have no idea what and I have done a lot of googling around on it. I suspect it's something to do with my PYTHONPATH but I really don't understand how to fix it.
Programming homework and newbie help thread Quote
04-04-2017 , 12:22 PM
Same issue solved and explained here: http://stackoverflow.com/questions/1...der-python-ide
Programming homework and newbie help thread Quote
04-04-2017 , 12:57 PM
I saw that and tried it, and it doesn't work, but there is a really good chance I'm doing something wrong. I'll explain what I did.

I went to the terminal and typed:
Code:
python
import matplotlib
matplotlib
Results:
Code:
<module 'matplotlib' from '/home/mdodd/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.pyc'>
So, I go to Spyder->Tools->PYTHONPATH manager and add a path to:
Code:
/home/mdodd/anaconda2/lib/python2.7/site-packages/matplotlib
I don't understand this "You'll then need to update the module names list from the same menu and restart spyder before the changes take effect." from the link you provided as I don't see where you would perform this update.

After adding this path I restart Spyder and when I run the code I get this error:
Code:
runfile('/home/mdodd/markstuff/DataScience From Scratch/chapter3_1.py', wdir='/home/mdodd/markstuff/DataScience From Scratch')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'runfile' is not defined
And, in fact, I get that error message for all of my python scripts.
Code:
def SomeFunction(k):
    x = 0
    for i in xrange(1,k+1):
        x += i
        print "i = %d, x = %d" % (i,x)
    print x
    return

SomeFunction(10)
Even this simple script throws the exact same error message. So, I gave up on that link, because even though it makes sense it seems to totally break Spyder for me and I don't understand what it is breaking that stops runfile from executing the script.


edit:
I didn't try this solution:
Quote:
Find the location of a module in Terminal:
$ python # open python

import pygame # import a module

pygame # get the location
Copy-paste the module folder to the 'Spyder.app/Contents/Resources/lib/python2.7'
Relaunch Spyder.app
As copying modules around the os seems like a very clunky solution and doesn't seem necessary, but maybe I'm wrong.

edit2:
I see the "Update Module Names List" and I clicked it and restarted Spyder and nothing changed.
Programming homework and newbie help thread Quote
04-11-2017 , 06:03 AM
I got this question today, not a clue, so, STATUS_REG_ADDR and DATA_REG_ADDR points to 32 bit registers and so *(u32int_t*)STATUS_REG_ADDR casts it as an address and accesses the value of the address, checks the bit and waits until the busy bit is 0, but for some reason, setgfaults.

I swear you are allowed to cast stuff as an address and access its value like *(u32int_t*)


What is wrong with the following code, and how would you fix it?

// Memory-mapped peripheral
#define STATUS_REG_ADDR 0x12345678 // 32-bit status register
#define DATA_REG_ADDR 0x1234567C // 32-bit data register

// Status register bits
#define BUSY_BIT_MASK 0x00000080 // Busy bit == '1' while peripheral busy

uint32_t get_value()
{
while (((*(uint32_t*)STATUS_REG_ADDR) & BUSY_BIT_MASK) == 1)
;

return *(uint32_t*)DATA_REG_ADDR;
}
Programming homework and newbie help thread Quote
04-11-2017 , 06:09 AM
Quote:
Originally Posted by CyberShark93
I got this question today, not a clue, so, STATUS_REG_ADDR and DATA_REG_ADDR points to 32 bit registers and so *(u32int_t*)STATUS_REG_ADDR casts it as an address and accesses the value of the address, checks the bit and waits until the busy bit is 0, but for some reason, setgfaults.

I swear you are allowed to cast stuff as an address and access its value like *(u32int_t*)


What is wrong with the following code, and how would you fix it?

// Memory-mapped peripheral
#define STATUS_REG_ADDR 0x12345678 // 32-bit status register
#define DATA_REG_ADDR 0x1234567C // 32-bit data register

// Status register bits
#define BUSY_BIT_MASK 0x00000080 // Busy bit == '1' while peripheral busy

uint32_t get_value()
{
while (((*(uint32_t*)STATUS_REG_ADDR) & BUSY_BIT_MASK) == 1)
;

return *(uint32_t*)DATA_REG_ADDR;
}
so i guess it segfaults, because the process is accessing memory that its not assigned to, but in that case how would you fix it?
Programming homework and newbie help thread Quote
04-11-2017 , 09:49 AM
That should work fine. What do you mean the process is not assigned to the memory?
Programming homework and newbie help thread Quote
04-11-2017 , 11:03 AM
Quote:
Originally Posted by Lattimer
That should work fine. What do you mean the process is not assigned to the memory?
I thought that should work fine too, but when I compile and run it, it segmentation faults.

I'm guessing it segfaults, because the process is not allowed to access the memory at the address we defined.but i'm not sure. its a question in a written test, so something is wrong, but I don't really know why
Programming homework and newbie help thread Quote
04-11-2017 , 11:43 AM
Oh I see it!

Instead of == 1 you want either == BUSY_BIT_MASK, != 0, or just no == at all. As is it will always evaluate as false, because the left side will always evaluate to either 0x00 or 0x80, never 1. Because of this, you're not waiting for busy to clear, and I imagine the target is erroring when attempting access of DATA_REG while busy.
Programming homework and newbie help thread Quote
04-11-2017 , 03:09 PM
Yeah your segfault is a red herring - they just gave an arbitrary address but in real life you would be allowed to hit that memory. You could demonstrate the actual problem by allocing 2 32 bit ints yourself and using those addresses instead.
Programming homework and newbie help thread Quote
04-13-2017 , 01:12 PM
Quote:
Originally Posted by CyberShark93
I got this question today, not a clue, so, STATUS_REG_ADDR and DATA_REG_ADDR points to 32 bit registers and so *(u32int_t*)STATUS_REG_ADDR casts it as an address and accesses the value of the address, checks the bit and waits until the busy bit is 0, but for some reason, setgfaults.

I swear you are allowed to cast stuff as an address and access its value like *(u32int_t*)


What is wrong with the following code, and how would you fix it?

// Memory-mapped peripheral
#define STATUS_REG_ADDR 0x12345678 // 32-bit status register
#define DATA_REG_ADDR 0x1234567C // 32-bit data register

// Status register bits
#define BUSY_BIT_MASK 0x00000080 // Busy bit == '1' while peripheral busy

uint32_t get_value()
{
while (((*(uint32_t*)STATUS_REG_ADDR) & BUSY_BIT_MASK) == 1)
;

return *(uint32_t*)DATA_REG_ADDR;
}
okay, got some feedback, there were 2 problems, one is as someone stated above, you don't need the ==1, since it always evaluates to 0x80 or 0x00, but never 1.

the second problem is when the compiler sees the condition inside the while, its going to assume it never changes, because nothing in the code changes the condition of the while, so you need to use the volatile key word to indicate to the compiler that the value inside these registers are being changed elsewhere.
Programming homework and newbie help thread Quote
04-13-2017 , 01:21 PM
That's build dependent. I build with 2 different compilers, and the default setting on 1 won't optimize out the while loop even with no volatile but on the other one it will. There are usually different optimization flags you can set if needed.
Programming homework and newbie help thread Quote
04-14-2017 , 05:21 PM
so, I got this question today, implement the function partial_invert which inverts the first 8 bits of an 32 bit int with the last 8 bits

my solution is
Spoiler:


uint32_t partial_invert(uint32_t input){

int msb = 31;

for(int i=0;i<8;i++){

t = (input>>(msb-i))&1;

input = ((input>>i)&1) ? input | (1<<(msb-i)) : input & (~(1<<(msb-i)));

input = t ? input | (1<<i) : input & (~(1<<i));

}
return input;
}



which they told me was 'fine', but they also said that it can be done in one line, I can't think of anyway to do it in one line except to do a lookup table(which is kinda cheating no?)
Programming homework and newbie help thread Quote
04-14-2017 , 06:40 PM
probably tricks with XOR ?

edit: no, I misunderstood your use of "invert" I think

Last edited by _dave_; 04-14-2017 at 06:51 PM.
Programming homework and newbie help thread Quote
04-14-2017 , 07:11 PM
Looking at what your code is doing, is the problem to...
- swap the 0th bit with the 31st
- swap the 1st bit with the 30th
- etc...
- down to swapping the 7th bit with the 24th?

So you're reversing and swapping the first and last bytes with each other?
Programming homework and newbie help thread Quote

      
m