low-level

How exactly does the callstack work?

可紊 提交于 2019-11-28 02:36:43
I'm trying to get a deeper understanding of how the low level operations of programming languages work and especially how they interact with the OS/CPU. I've probably read every answer in every stack/heap related thread here on Stack Overflow, and they are all brilliant. But there is still one thing that I didn't fully understand yet. Consider this function in pseudo code which tends to be valid Rust code ;-) fn foo() { let a = 1; let b = 2; let c = 3; let d = 4; // line X doSomething(a, b); doAnotherThing(c, d); } This is how I assume the stack to look like on line X: Stack a +-------------+

Would you use num%2 or num&1 to check if a number is even?

半腔热情 提交于 2019-11-28 02:33:07
问题 Well, there are at least two low-level ways of determining whether a given number is even or not: 1. if (num%2 == 0) { /* even */ } 2. if ((num&1) == 0) { /* even */ } I consider the second option to be far more elegant and meaningful, and that's the one I usually use. But it is not only a matter of taste; The actual performance may vary: usually the bitwise operations (such as the logial-and here) are far more efficient than a mod (or div) operation. Of course, you may argue that some

Shifting a 32 bit integer by 32 bits

好久不见. 提交于 2019-11-27 23:19:47
I'm slinging some C code and I need to bitshift a 32 bit int left 32 bits. When I run this code with the parameter n = 0, the shifting doesn't happen. int x = 0xFFFFFFFF; int y = x << (32 - n); Why doesn't this work? Shift at your own peril. Per the standard, what you want to do is undefined behavior . C99 §6.5.7 3 - The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined. In other words

What is the 'shadow space' in x64 Assembly?

 ̄綄美尐妖づ 提交于 2019-11-27 19:18:14
问题 I found plenty of topics about this shadow space, but I couldn't find the answer in none of them, so my question is: How much exactly bytes I need to subtract from the stack pointer, before entering to a procedure? And should I push the procedure parameters to the stack before subtracting the "shadow space"? I've disassembled my code, but I couldn't find the logic. 回答1: The Shadow space (also sometimes called Spill space or Home space ) is meant to be used, to make debugging x64 easier.

CPU Emulation and locking to a specific clock speed

烂漫一生 提交于 2019-11-27 17:52:50
If you had read my other question , you'll know I've spent this weekend putting together a 6502 CPU emulator as a programming exercise. The CPU emulator is mostly complete, and seems to be fairly accurate from my limited testing, however it is running incredibly fast, and I want to throttle it down to the actual clock speed of the machine. My current test loop is this: // Just loop infinitely. while (1 == 1) { CPU.ClockCyclesBeforeNext--; if (CPU.ClockCyclesBeforeNext <= 0) { // Find out how many clock cycles this instruction will take CPU.ClockCyclesBeforeNext = CPU.OpcodeMapper.Map[CPU

Why doesn't Linux use the hardware context switch via the TSS?

北城以北 提交于 2019-11-27 17:50:00
I read the following statement: The x86 architecture includes a specific segment type called the Task State Segment (TSS), to store hardware contexts. Although Linux doesn't use hardware context switches, it is nonetheless forced to set up a TSS for each distinct CPU in the system. I am wondering: Why doesn't Linux use the hardware support for context switch? Isn't the hardware approach much faster than the software approach? Is there any OS which does take advantage of the hardware context switch? Does windows use it? At last and as always, thanks for your patience and reply. -----------Added

Why do you program in assembly? [closed]

只谈情不闲聊 提交于 2019-11-27 16:40:29
I have a question for all the hardcore low level hackers out there. I ran across this sentence in a blog. I don't really think the source matters (it's Haack if you really care) because it seems to be a common statement. For example, many modern 3-D Games have their high performance core engine written in C++ and Assembly. As far as the assembly goes - is the code written in assembly because you don't want a compiler emitting extra instructions or using excessive bytes, or are you using better algorithms that you can't express in C (or can't express without the compiler mussing them up)? I

How does the computer calculate Square roots? [closed]

情到浓时终转凉″ 提交于 2019-11-27 15:42:09
问题 How does the computer calculate Square roots ? I mean what is going on there! How does it process it!! Does it use some mathematical ways like Newton's method? What about Trigonometric Functions? And almost all those Mathematical Functions . In the case that every language has its own way, then please let's talk about c++. 回答1: Most modern non-embedded CPUs (x86 and the larger ARM cores, for example) have hardware instructions to compute square roots directly. The hardware implementation

To learn assembly - should I start with 32 bit or 64 bit?

你离开我真会死。 提交于 2019-11-27 09:01:56
问题 I'm really wanting to learn assembly. I'm pretty good at c/c++, but want a better understanding of what's going on at a lower level. I realize that assembly related questions have been asked before, but I'm just looking for some direction that's particular to my situation: I'm running windows 7, and am confused about how I should start working with assembly. Do I have to start with x64 because I'm running windows 7? Some people have said 'start with 32 bit first' - how do I go about doing

How exactly does the callstack work?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 04:59:14
问题 I'm trying to get a deeper understanding of how the low level operations of programming languages work and especially how they interact with the OS/CPU. I've probably read every answer in every stack/heap related thread here on Stack Overflow, and they are all brilliant. But there is still one thing that I didn't fully understand yet. Consider this function in pseudo code which tends to be valid Rust code ;-) fn foo() { let a = 1; let b = 2; let c = 3; let d = 4; // line X doSomething(a, b);