Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Gotchas

6
Task/Gotchas/00-TASK.txt Normal file
View file

@ -0,0 +1,6 @@
;Definition
In programming, a [[wp:Gotcha_(programming)|'''gotcha''']] is a valid construct in a system, program or programming language that works as documented but is counter-intuitive and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome.
;Task
Give an example or examples of common gotchas in your programming language and what, if anything, can be done to defend against it or them without using special tools.
<br><br>

View file

@ -0,0 +1,5 @@
LDA 'J' ;load the 8-bit value stored at memory address 0x004A into the accumulator.
OR 3 ;bitwise OR the accumulator with the 8-bit value stored at memory address 0x0003
LDA #'7' ;load the ASCII code for the numeral 7 (which is 0x37) into the accumulator.

View file

@ -0,0 +1,5 @@
LDA #$20
STA $30FF
LDA #$40
STA $3100
JMP ($30FF) ;rather than take the high byte from $3100, the high byte is taken from $3000 instead.

View file

@ -0,0 +1 @@
byte $45 ;this is the literal constant value $45, not "the value stored at memory address 0x0045"

View file

@ -0,0 +1,2 @@
INC $2005 ;the intent was to scroll the NES's screen to the right one pixel. That's not gonna happen.
;What actually happens? Who knows! (I made this mistake once long ago.)

View file

@ -0,0 +1,3 @@
LDA #$20
CMP #$19
BCS foo ;this branch is always taken, since #$20 >= #$19. If this were any other CPU this branch would never be taken!

View file

@ -0,0 +1,3 @@
LDA #8
SEC
SBC #4 ;eight minus four

View file

@ -0,0 +1,2 @@
CLC
ADC ___ ;your operand/addressing mode of choice goes here

View file

@ -0,0 +1,2 @@
LDX #$FF
LDA $80,X ;loads from address $007F, not $017F

View file

@ -0,0 +1 @@
LDA $2080,X ;loads from the correct address regardless of the value of X.

View file

@ -0,0 +1,5 @@
LDA #$20
STA $3000
LDA #$40
STA $3001
JMP ($3000) ;evaluates to JMP $4020

View file

@ -0,0 +1,2 @@
MOVE.L $12345678,D0 ;move the 32-bit value stored at memory address $12345678 into D0
MOVE.L #$12345678,D0 ;load the D0 register with the constant value $12345678

View file

@ -0,0 +1 @@
DC.B $45 ;this is the literal constant value $45, not "the value stored at memory address 0x0045"

View file

@ -0,0 +1,4 @@
MOVEA.L #$A04000,A0 ;load the address $A04000 into A0
MOVE.L A0,D0 ;move the quantity $A04000 into D0
MOVE.B (A0),D0 ;get the 8-bit value stored at memory address $A04000, and store it into the lowest byte of D0.
MOVE.W (4,A0),D1 ;get the 16-bit value stored at memory address $A04004, and store it into the low word of D1.

View file

@ -0,0 +1,2 @@
LEA $A04000,A0 ;effectively MOVEA.L #$A04000,A0
LEA (4,A0),A0 ;effectively ADDA.L #4,A0

View file

@ -0,0 +1,6 @@
MOVE.L #$12345678,D0 ;set the entire register to a known value for demonstration purposes.
MOVE.W #$7FFF,D0 ;D0 = $12347FFF
ADD.W #1,D0 ;D0 = $12348000
TRAPV ;the above operation set the overflow flag, so this instruction will call the signed overflow handler.
;Even though the entire register didn't overflow, the portion we were operating on did, so that counts.

View file

@ -0,0 +1,4 @@
MOVE.B #16-1,D0 ;loop 16 times
forloop:
; loop body goes here
DBRA D0,forloop

View file

@ -0,0 +1,2 @@
MOVEA.W #$8000,A0 ;MOVEA.L #$FF8000,A0
MOVEA.W #$7FFF,A0 ;MOVEA.L #$007FFF,A0

View file

@ -0,0 +1,2 @@
MOVE.W #$FF,D0 ;MOVE.W #$00FF,D0
MOVE.L #$8000,D2 ;MOVE.L #$00008000,D2

View file

@ -0,0 +1,5 @@
MOVE.W #$FF,D0
EXT.W D0 ;D0 = $????FFFF
MOVE.L #$8000,D1
EXT.L D1 ;D1 = $FFFF8000

View file

@ -0,0 +1,3 @@
if(a=b){} //assigns to "a" the value of "b". Then, if "a" is nonzero, the code in the curly braces is run.
if(a==b){} //runs the code in the curly braces if and only if the value of "a" equals the value of "b".

View file

@ -0,0 +1,7 @@
int main()
{
int x = 3;
int y = 5;
int z = 7;
printf("%d %d %d %x %x",x,y,z); //on an Intel cpu the first %x reveals %%ebp and the second reveals the return address.)
}

View file

@ -0,0 +1 @@
int foo[4] = {4,8,12,16};

View file

@ -0,0 +1,4 @@
int foo[4] = {4,8,12,16};
int x = foo[0]; //x = 4
int y = foo[3]; //y = 16
int z = foo[4]; //z = ?????????

View file

@ -0,0 +1,5 @@
int foo()
{
char bar[20];
return sizeof(bar);
}

View file

@ -0,0 +1,7 @@
int foo()
{
#define size_of_bar 20 //the sizeof operator is the same as doing this essentially.
char bar[size_of_bar];
return size_of_bar;
}

View file

@ -0,0 +1,4 @@
int gotcha(char bar[])
{
return sizeof(bar);
}

View file

@ -0,0 +1,2 @@
myArray[40];
int x = gotcha(myArray);

View file

@ -0,0 +1,2 @@
myArray[40];
int x = gotcha(&myArray[0]);

View file

@ -0,0 +1,7 @@
int foo(char buf[],int length){}
int main()
{
char myArray[30];
int j = foo(myArray,sizeof(myArray)); //passes 30 as the length parameter.
}

View file

@ -0,0 +1,10 @@
ex1=: 1 2 3 4 5
ex2=: '1 2 3 4 5'
ex1
1 2 3 4 5
ex2
1 2 3 4 5
10+ex1
11 12 13 14 15
10+ex2
|domain error

View file

@ -0,0 +1,8 @@
1 2 3 + 4 5 6
5 7 9
+/ 1 2 3 + 4 5 6
21
1 2 3 +/@:+ 4 5 6
21
1 2 3 +/@+ 4 5 6
5 7 9

View file

@ -0,0 +1,6 @@
10 100 1000 +/ .*1 2 3 NB. vector multiplication
3210
10 100 1000 +/.*1 2 3 NB. complex conjugate of signum of right argument grouped by the left argument
1
1
1

View file

@ -0,0 +1,11 @@
A=: 2 3 5 7 11 NB. legal
B=: 999 NB. legal
B 0} A NB. functional array update (does not change A)
NB. the use of a single brace to denote indexing might also confuse people
999 3 5 7 11
0} A NB. legal
2
999 0} A NB. not legal
|rank error
(999)0} A NB. what was intended
999 3 5 7 11

View file

@ -0,0 +1,14 @@
0x100 NB. 0*e^100 where e is the natural logarithm base 2.71828...
0
0xff
|ill-formed number
16b100 16bff
256 255
8ad90 NB. 8 on the complex plane at an angle of 90 degrees from the real axis
0j8
-100 NB. two tokens
_100
_100+10 NB. three tokens
_90
-100+10 NB. four tokens
_110

View file

@ -0,0 +1,4 @@
1+2*3 NB. processed right-to-left
7
2*3+1 NB. processed right-to-left
8

View file

@ -0,0 +1,3 @@
move $t0,$zero ;load 0 into $t0
beqz $t0,myLabel ;branch if $t0 equals 0
addiu $t0,1 ;add 1 to $t0. This happens during the branch, even though the program counter never reaches this instruction.

View file

@ -0,0 +1,3 @@
la $a0,0xDEADBEEF
lw $t0,($a0) ;load the 32-bit value at memory address 0xDEADBEEF
addiu $t0,5 ;5 is actually added BEFORE the register has gotten its new value from the memory load above. It will be clobbered.

View file

@ -0,0 +1,12 @@
sub array1 { return @{ [ 1, 2, 3 ] } }
sub list1 { return qw{ 1 2 3 } }
# both print '3', but why exactly?
say scalar array1();
say scalar list1();
sub array2 { return @{ [ 3, 2, 1 ] } }
sub list2 { return qw{ 3 2 1 } }
say scalar array2(); # prints '3', number of elements in array
say scalar list2(); # prints '1', last item in list

View file

@ -0,0 +1,9 @@
(phixonline)-->
<span style="color: #008080;">forward</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">o</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">hello</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hello"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">hello</span> <span style="color: #000080;font-style:italic;">-- fatal error: hello has not been assigned a value</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<!--

View file

@ -0,0 +1,3 @@
say 123 ~ 456; # join two integers together
say "12" + "5.7"; # add two numeric strings together
say .sqrt for <4 6 8>; # take the square root of several allomorphic numerics

View file

@ -0,0 +1,3 @@
say my $bag = <a b a c a b d>.Bag;
say $bag{'a'}; # a count?
say $bag< a >; # another way

View file

@ -0,0 +1,5 @@
say my $bag = (1, '1', '1', <1 1 1>).Bag;
say $bag{ 1 }; # how many 1s?
say $bag{'1'}; # wait, how many?
say $bag< 1 >; # WAT
dd $bag; # The different numeric types LOOK the same, but are different types behind the scenes

View file

@ -0,0 +1 @@
.say for (1,2,3), (4,5,6);

View file

@ -0,0 +1 @@
.say for (1,2,3);

View file

@ -0,0 +1 @@
.say for (1,2,3),;

View file

@ -0,0 +1 @@
.say for flat (1,2,3), (4,5,6);

View file

@ -0,0 +1,33 @@
class Rectangle {
construct new(width, height) {
// Create two fields.
_width = width
_height = height
}
area {
// Here we mis-spell _width.
return _widht * _height
}
isSquare {
// We inadvertently use '=' rather than '=='.
// This sets _width to _height and will always return true
// because any number (even 0) is considered 'truthy' in Wren.
if (_width = _height) return true
return false
}
diagonal {
// We use 'sqrt' instead of the Math.sqrt method.
// The compiler thinks this is an instance method of Rectangle
// which will be defined later.
return sqrt(_width * _width + _height * _height)
}
}
var rect = Rectangle.new(80, 100)
System.print(rect.isSquare) // returns true which it isn't!
System.print(rect.area) // runtime error: Null does not implement *(_)
System.print(rect.diagonal) // runtime error (if previous line commented out)
// Rectangle does not implement 'sqrt(_)'

View file

@ -0,0 +1,4 @@
label:
;loop body goes here
DEC ECX
JNZ label

View file

@ -0,0 +1,5 @@
LD HL,(&C000) ;load the word at address &C000 into HL
LD A,(HL) ;treating the value in HL as a memory address, load the byte at that address into A.
EX (SP),HL ;exchange HL with the top two bytes of the stack.
JP (HL) ;set the program counter equal to HL. Nothing is loaded from memory pointed to by HL.

View file

@ -0,0 +1,3 @@
ld a,&46
ld bc,&0734
out (C),a ;write &46 to port &0734 if the ports are 16-bit. Otherwise, it writes to port &34.

View file

@ -0,0 +1,2 @@
EI ;RETI doesn't enable interrupts on this Z80.
RET