﻿ 王爽《汇编语言》探究实验五——自己实现一个printf函数 - 鸿网互联

王爽《汇编语言》探究实验五——自己实现一个printf函数

void myprintf (char *, ...);int length;void main () {myprintf (abcdefg%h%%%dabcde%c, -123, 'm');}void myprintf (char *chars, ...) {int i = 0;int anum=0, anumcopy = 0, yu=0, yulen=0;int yuss[9];int *yus=yuss;int length;int position = 0;int
```void myprintf (char *, ...);
int length;
void main () {
myprintf ("abcdefg%h%%%dabcde%c", -123, 'm');
}
void myprintf (char *chars, ...) {
int i = 0;
int anum=0, anumcopy = 0, yu=0, yulen=0;
int yuss[9];
int *yus=yuss;
int length;
int position = 0;
int ssposi = 0;
char a;
length = 0;
a = chars[0];
while (a != '\0') {		//取得格式控制字符串的长度；
length++;
a = chars[length];
}
while (i < length) {
if (*(char *)(0x0c3c0194+i) == '%') {		//对%的识别；
if (*(char *)(0x0c3c0194+i+1)=='c') {	//识别%c;
*(char far *)(0xb8000000+160*10+position) = *(int *)(_BP+6+ssposi);
i+=2;
ssposi+=2;
} else if (*(char *)(0x0c3c0194+i+1)=='d') {	//识别%d；
anum = *(int *)(_BP+6+ssposi);
anumcopy = anum;

if ((anumcopy&32768) == 32768) {
anum = (~anum);
anum+=1;
while (anum != 0) {
yu = anum%10;
*(yus+yulen) = yu;
anum /= 10;
yulen++;
}
yulen = yulen-1;
*(char far *)(0xb8000000+160*10+position)=0x2d;
position+=2;
while (yulen >= 0) {
*(char far *)(0xb8000000+160*10+position)=*(yus+yulen)+0x30;
yulen--;
position+=2;
}
position = position-2;

ssposi+=2;
} else {
while (anum != 0) {
yu = anum%10;
*(yus+yulen) = yu;
anum /= 10;
yulen++;
}

yulen = yulen-1;

while (yulen >= 0) {
*(char far *)(0xb8000000+160*10+position)=*(yus+yulen)+0x30;
yulen--;
position+=2;
}
position = position-2;

ssposi+=2;
}
i+=2;

} else {
*(char far *)(0xb8000000+160*10+position) = *(char *)(0x0c3c0194+i);
*(char far *)(0xb8000000+160*10+position+2) = *(char *)(0x0c3c0194+i+1);
i++;
}
} else {
*(char far *)(0xb8000000+160*10+position) = *(char *)(0x0c3c0194+i);
i++;
}
position+=2;
}

}

```

<