4

//PTRD = 100, PTRA = 100
typedef struct
{
int width;
int height;
}Size;

typedef struct {
int x;
int y;
}Point;

bool clipLine(Size img_size, Point &pt1, Point &pt2)
{
int x1, y1, x2, y2;
int c1, c2;
int right = img_size.width - 1, bottom = img_size.height - 1;

if (img_size.width <= 0 || img_size.height <= 0)
return false;
x1 = pt1.x; y1 = pt1.y;
x2 = pt2.x; y2 = pt2.y;

c1 = x1 < 0 + (x1 > right) * 2 + (y1 < 0) * 4 + (y1 > bottom) * 8;
c2 = x2 < 0 + (x2 > right) * 2 + (y2 < 0) * 4 + (y2 > bottom) * 8;

if ((c1 & c2) == 0 && (c1 | c2) != 0) {
int a;
if (c1 & 12) {
a = c1 < 8 ? 0 : bottom;
x1 += (int)(((int64) ( a - y1 )) * (x2 - x1) / (y2 - y1));
y1 = a;
c1 = (x1 < 0) + (x1 > right) * 2;
}
...
}
}

int main(void)
{
Size img = {10,10};
Point t1 = {1,1}, t2 = {18, 8};
clipLine(img, t1, t2);
return 0;

}

// 翻译成汇编该怎么做？下面的翻译的对不对？该怎么解释？？谢谢各位大神

MOV (0x400), (0x06) ;参数地址
MOV (0x401), (0x07)
MOV (0x402), (0x08)
MOV (0x403), (0x09)
MOV (0x404), (0x0a)
MOV (0x405), (0x0b)
CALL FUNC          ;绝对地址
...
FUNC: MOV (2), PTR2 ;存储调用者的基地址
SUBI (12), (0x400), PTR3
SUBI (13), (0x401), PTR3
MOV (6), (0x402)
MOV (7), (0x403)
MOV (8), (0x404)
MOV (9), (0x405)
BLT (6), PTR0, #X11
MOV R1, PTR0
X1RIGHT: BGT (6), (12), #X12
MOV R2, PTR0
Y10: BLT (7), PTR0, #Y11
MOV R3, PTR0
Y1BOTTOM: BLT (7), (13), #Y81
MOV R4, PTR0
JUMP #SUM
JUMP #X1RIGHT
JUMP #Y10
JUMP #Y1BOTTOM
BLT (8), PTR0, #X11
MOV R1, PTR0
X2RIGHT: BGT (8), (12), #X21
MOV R2, PTR0
Y20: BLT (9), PTR0, #Y21
MOV R3, PTR0
Y2BOTTOM: BLT (9), (13), #YB81
MOV R4, PTR0
JUMP #SUM
JUMP #X2RIGHT
JUMP #Y20
JUMP #Y2BOTTOM
AND R4, (10), (11)
BNE R4, PTR0, #NON_CLIP
OR R4, (10), (11)
BEQ R4, PTR0, #NON_CLIP
BEQ R4, PTR0, #C212

C212: ...
NON_CLIP1: MOV PTR1, (2)  ;恢复调用者的基地址
RET

objdump
--- 共有 9 条评论 ---

--- 共有 5 条评论 ---

cut回复 @超级大坏蛋 : gcc编译器 gcc -s icc编译器 icl /O2 /s hello.c vc编译器 /FAs 手写汇编就一个蛋疼，不过你搞编译器的，最好还是手写汇编体验下，一般编译器不是必须还是有汇编器的么，你先在你的汇编器上写汇编看看。 6年前